will some please provide a simple example of how
algorithm is used?Code:sort(begin, end, op)
Thanks.
This is a discussion on sort(begin,end,op) stl algorithm within the C++ Programming forums, part of the General Programming Boards category; will some please provide a simple example of how Code: sort(begin, end, op) algorithm is used? Thanks....
will some please provide a simple example of how
algorithm is used?Code:sort(begin, end, op)
Thanks.
Last edited by kes103; 04-20-2003 at 04:54 PM.
Code:vector v; v.push_back( 3 ); v.push_back( 1 ); v.push_back( 8 ); sort( v.begin( ), v.end( ) );
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
Thanks, but would like to know how and/or receive an example of
how to use the "op" option in sort(v.begin(), v.end(),op).
op is the binary predicate should be op(elem1, elem2).
I need an example please. elem1 and elem2 are sorting criteria
but that is all I know.
Please help.
Last edited by kes103; 04-20-2003 at 08:06 PM.
Thanks, but would like to know how and/or receive an example of
how to use the "op" option in sort(v.begin(), v.end(),op).
op is the binary predicate should be op(elem1, elem2).
I need an example please. elem1 and elem2 are sorting criteria
but that is all I know.
Please help.Code:vector<int> v; v.push_back(5); v.push_back(3); v.push_back(17); // The following are all identical in how they sort the vector, least to greatest sort(v.begin(),v.end()); // Sorts using default less<int>() or '<' operator sort(v.begin(),v.end(),mysort1); // Sorts using user defined function sort(v.begin(),v.end(),less<int>()); // Explicit use of less<int> copy(v.begin(),v.end(),ostream_iterator<int>(cout," ")); // Outputs 3 5 17 // The following are all identical in how they sort the vector, greatest to least sort(v.rbegin(),v.rend()); // Sorts using default less<int>() but in reverse order sort(v.begin(),v.end(),mysort2); // Sorts using user defined function sort(v.begin(),v.end(),greater<int>()); // Explicit use of greater<int> copy(v.begin(),v.end(),ostream_iterator<int>(cout," ")); // Outputs 17 5 3 ... bool mysort1( const int& i1, const int& i2 ) { return i1 < i2; } bool mysort2( const int& i1, const int& i2 ) { return i1 > i2; }
Last edited by hk_mp5kpdw; 04-22-2003 at 11:01 AM.
I used to be an adventurer like you... then I took an arrow to the knee.