Форум » C/C++ для начинающих (C/C++ for beginners) » How to Group a value inside the Array based on high value and low value » Ответить

How to Group a value inside the Array based on high value and low value

ramees: i have a random number dynamic array in my code i need to filter my array. See below: link http://tinypic.com/view.php?pic=28iwzz9&s=8#.U3881HL7Jt8 int ary[]={2 ,5,6,7,5,4,2,1 , 3 ,3,6,10,9,6,2,1 }; i need output like this : out 1: 2 , 5 , 6 , 7 , 5 , 4 , 2 , 1 out 2: 3 , 3 , 6 ,10, 9 , 6 , 2 , 1 if my array like int a[] = { 1, 2, 1, 1, 1, 3 }; out 1: 1, 2, 1, 1, 1 out 2: 3, if my array like int a[] = { 1, 2, 1, 2, 1, 3 }; out 1: 1, 2, 1, out 2: 2, 1, out 3: 3, first largest value and its surrounding value group together and so on......

Ответов - 21, стр: 1 2 All

Сыроежка: Well, You can use standard algorithm std::adjacent_find declared in header <algorithm>. For example [pre2] #include <iostream> #include <algorithm> #include <iterator> #include <functional> int main() { int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 }; int *p = std::begin( a ); while ( p != std::end( a ) ) { int *q = std::adjacent_find( p, std::end( a ), std::greater<int>() ); q = std::adjacent_find( q, std::end( a ), std::less<int>() ); if ( q != std::end( a ) ) ++q; std::copy( p, q, std::ostream_iterator<int>( std::cout, " " ) ); std::cout << std::endl; p = q; } return 0; } [/pre2] The output is [pre2] 2 5 6 7 5 4 2 1 3 3 6 10 9 6 2 1 [/pre2] You can check the code using the on-line compiler at www.ideone.com. However take into account that in the original example at www.stackoverflow you showed another logic of splitting an array when there are adjacent equal elements. The other way to do the task is to use standard algorithm std::is_sorted_until.

ramees: thanks how can i add this outputs : out 1 : 2 5 6 7 5 4 2 1 out 2 : 3 3 6 10 9 6 2 1 into a dynamic array i don't no how this section works std::copy( p, q, std::ostream_iterator<int>( std::cout, " " ) );

Сыроежка: Construction [pre2] std::copy( p, q, std::ostream_iterator<int>( std::cout, " " ) ); [/pre2] is a call of standard algorithm std::copy that copies elements in the range [p, q) into standard output stream. In fact [pre2] std::ostream_iterator<int>( std::cout, " " ) [/pre2] is equivalent to [pre2] std::cout << *p << " "; [/pre2] for each p that preceeds to q. You could write instead for example [pre2] for ( auto t = p; t != q; ++t ) std::cout << *t << " "; [/pre2] As for your second question then it is better and simpler to use standard container std::vector instead of using dynamically allocated arrays. For example [pre2] #include <iostream> #include <algorithm> #include <iterator> #include <functional> #include <vector> int main() { int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 }; int *p = std::begin( a ); std::vector<std::vector<int>> v; while ( p != std::end( a ) ) { int *q = std::adjacent_find( p, std::end( a ), std::greater<int>() ); q = std::adjacent_find( q, std::end( a ), std::less<int>() ); if ( q != std::end( a ) ) ++q; v.push_back( std::vector<int>() ); v.back().reserve( std::distance( p, q ) ); std::copy( p, q, std::back_inserter( v.back() ) ); p = q; } for ( const std::vector<int> &v1 : v ) { std::copy( v1.begin(), v1.end(), std::ostream_iterator<int>( std::cout, " " ) ); std::cout << std::endl; } return 0; } [/pre2] If to use dynamically allocated arrays then you should at first count how many there are subsequences in the original sequence. It means that the while loop has to be executed twice.


ramees: yes this will work

Сыроежка: I am sorry. The code could be written simpler [pre2] #include <iostream> #include <algorithm> #include <iterator> #include <functional> #include <vector> int main() { int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 }; auto first = std::begin( a ); std::vector<std::vector<int>> v; while ( first != std::end( a ) ) { auto last = std::adjacent_find( first, std::end( a ), std::greater<int>() ); last = std::adjacent_find( last, std::end( a ), std::less<int>() ); if ( last != std::end( a ) ) ++last; v.push_back( std::vector<int>( first, last ) ); first = last; } for ( const std::vector<int> &v1 : v ) { std::copy( v1.begin(), v1.end(), std::ostream_iterator<int>( std::cout, " " ) ); std::cout << std::endl; } return 0; } [/pre2]

ramees: how about this one #include <iostream> #include <algorithm> #include <iterator> #include <functional> using namespace std; const int X = 1000; int main() { int *AryR; AryR = new int [X]; int cu = 0; int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 }; int *p = std::begin( a ); while ( p != std::end( a ) ) { int *q = std::adjacent_find( p, std::end( a ), std::greater<int>() ); q = std::adjacent_find( q, std::end( a ), std::less<int>() ); if ( q != std::end( a ) ) ++q; for ( auto t = p; t != q; ++t ) { // std::cout << *t << " " AryR[cu]=*t; cu++; p = q; } for(int i =0; i<cu;i++ ){ cout<<AryR[ i ]; } cout<<" "<<endl; cu = 0; } return 0; }

Сыроежка: It is a bad code.:) First of all it is not clear why the dynamically allocated array has 1000 elements. Secondly you overwrite each time the previously extracted subsequence.

ramees: ok then i will countine with vector i put this code into nuke plugin code its give me this error . no instance of overloaded function "std::adjacent_find" matches the argument list for screen capture imge pls see below link click here click here

Сыроежка: Please show error messages here. Do not use links because images are loaded slowly. In any case the error messages mean that you incorrectly specified arguments of the algorithm. See my example above and do the same way.

ramees: red struggle under line in all std:: when i hover over mouse this error pop up : no instance of overloaded function "std::adjacent_find" matches the argument list in this code i already using DD::image namespace i think this is some name space error i don't no how to tell so i took a print screen image for better understanding

Сыроежка: I showed you code. So you should use it as a template. If some errors occur then they are errors of your code. The code I showed is compiled without any error. I do not know what is DD::image namespace. It has nothing common with the code I showed.

ramees: is there any way to do this code without std algorithm

Сыроежка: Any standard algorithm is a regular function. So you can write yourself the corresponding function. For example this call [pre2] std::adjacent_find( first, std::end( a ), std::greater<int>() ); [/pre2] can be substituted for a call of the following function [pre2] const int * find_greater( const int a[], size_t n ) { if ( n != 0 ) { for ( const int *prev = a, *next = a; ++next != a + n; ++prev ) { if ( *next < *prev ) return prev; } } return ( a + n ); } [/pre2] The program can be wriiten as [pre2] #include <iostream> #include <vector> const int * find_greater( const int a[], size_t n ) { if ( n != 0 ) { for ( const int *prev = a, *next = a; ++next != a + n; ++prev ) { if ( *next < *prev ) return prev; } } return ( a + n ); } const int * find_less( const int a[], size_t n ) { if ( n != 0 ) { for ( const int *prev = a, *next = a; ++next != a + n; ++prev ) { if ( *prev < *next ) return prev; } } return ( a + n ); } int main() { int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 }; size_t n = sizeof( a ) / sizeof( *a ); const int *first = a; std::vector<std::vector<int>> v; while ( first != a + n ) { const int * last = find_greater( first, n - ( first - a ) ); last = find_less( last, n - ( last - a ) ); if ( last != a + n ) ++last; v.push_back( std::vector<int>( first, last ) ); first = last; } for ( const std::vector<int> &v1 : v ) { for ( int x : v1 ) std::cout << x << ' '; std::cout << std::endl; } return 0; } [/pre2] The output is [pre2] 2 5 6 7 5 4 2 1 3 3 6 10 9 6 2 1 [/pre2]

ramees: intelsense error because i am using visual studio 10 c++ is not fully support c++11 features i try to compile both version in ideone. older cpp error in this section below :in visual studio 10 Error red stuggle in " : " when i hover over mouse this error pop up -> referance variable "v1" requires an initializer for ( const std::vector<int> &v1 : v ) { for ( int x : v1 ) std::cout << x << ' '; std::cout << std::endl; } i can't use new version visual studio how can i do this in older c++

Сыроежка: MS VS 2010 has its own language extension for the range-based for statement. You may use the following construction [pre2] for each ( const std::vector<int> &v1 in v ) { for each ( int x in v1 ) std::cout << x << ' '; std::cout << std::endl; } [/pre2] Or you can use regular for statements either with an index or with an iterator. For example [pre2] for ( std::vector<std::vector<int>>::size_type i = 0; i < v.size(); i++ ) { for ( std::vector<int>::size_type j = 0; j < v.size(); j++ ) { std::cout << v[ i ][ j ] << ' '; } std::cout << std::endl; } for ( std::vector<std::vector<int>>::iterator it1 = v.begin(); it1 != v.end(); ++it1 ) { for ( std::vector<int>::iterator it2 = it1->begin(); it2 != it1->end(); ++it2 ) { std::cout << *it2 << ' '; } std::cout << std::endl; } [/pre2]

ramees: i seen a lot for each in my code i don't no how its work and what is the difference between foreach & for loop

Сыроежка: It simply sorts out elements in a container. For example [pre2] #include <iostream> int main() { int a[] = { 1, 2, 3, 4, 5 }; for each ( int x in a ) std::cout << x << ' '; std::cout << std::endl; } [/pre2]

ramees: i try to change array to vector but error how to do this in a vector

Сыроежка: I do not understand what array you are speaking about. In this thread there is shown enough code that demonstrates how to work with class std::vector. and how to do the task you asked about.

ramees: int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 }; in ur code we get data from array. i like to get data from vector<int> a; i try to change but error. sorry for asking same question again an again i have no other option to get right answers i learning c++ from youtube so i don't have any professor to ask .

Сыроежка: First of all I do not see any error. and do not know where the error occured. Any programmer can have many errors in his programs. But I do not think that we should discuss your errors in this thread. The question of the thread was answered with a detailed solution. You may create a new thread where you will describe your errors and present the corresponding code.



полная версия страницы