will some please provide a simple example of how
algorithm is used?Code:sort(begin, end, op)
Thanks.
Printable View
will some please provide a simple example of how
algorithm is used?Code:sort(begin, end, op)
Thanks.
Code:vector v;
v.push_back( 3 );
v.push_back( 1 );
v.push_back( 8 );
sort( v.begin( ), v.end( ) );
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.
Quote:
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;
}