Sort vector of user-defined values : object vector « Vector « C++






Sort vector of user-defined values

  
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <utility>

using namespace std;

typedef pair<int,string> Pair;

inline bool less_than_second( const Pair& b1, const Pair& b2 ){
   return b1.second < b2.second;
}

int main( )
{
   const char* names[] = { "A","B", "C", "D","E" };
   const int values[] = { 18, 20, 26, 30, 41 };
   const int num_pairs = sizeof( names ) / sizeof( names[0] );

   vector<Pair> pair( num_pairs );
   transform( values, values+num_pairs, names,pair.begin(), make_pair<int,string> );

   sort( pair.begin(), pair.end() );

   vector<Pair>::const_reverse_iterator pair_rend = pair.rend();
   
   for( vector<Pair>::const_reverse_iterator i= pair.rbegin(); i != pair_rend; ++i )
      cout << i->first << " - " << i->second;

   sort( pair.begin(), pair.end(), less_than_second );

   vector<Pair>::const_iterator pair_end = pair.end();
   for( vector<Pair>::const_iterator i = pair.begin();
      i != pair_end; ++i )
   cout << i->second << " - $" << i->first << " values\n";
}
  
    
  








Related examples in the same category

1.Call member function for each element in vector
2.Getting a C-Style Array from a Vector
3.Store user-defined objects in a vector.