Mega Code Archive

 
Categories / C++ Tutorial / STL Algorithms Modifying Sequence Operations
 

Transform array to pair

#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> );    if( pair[1].first > pair[3].first )       cout << pair[1].second << " > " << pair[3].second << endl;    else if( pair[1].first == pair[3].first )       cout << pair[1].second << " == " << pair[3].second << endl;    else       cout << pair[1].second << " < " << pair[3].second << endl; }