sort(begin,end,op) stl algorithm

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....

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    sort(begin,end,op) stl algorithm

    will some please provide a simple example of how
    Code:
    sort(begin, end, op)
    algorithm is used?
    Thanks.
    Last edited by kes103; 04-20-2003 at 04:54 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    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.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,675
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL Sort Algorithm
    By f1player in forum C++ Programming
    Replies: 9
    Last Post: 10-10-2008, 01:12 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Debuggin a Simple STL Algorithm :: STL
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2002, 10:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21