#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <utility>
using namespace std;
template <class T>
void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
std::cout << *i << endl;
}
}
int main( ){
const int num_cars = 6;
const int year[num_cars] = { 1998, 1970, 1966, 2004, 1998, 1930 };
const char* name[num_cars] = { "A", "B","C", "D", "E","F" };
set<int> unique_years( year, year + num_cars );
multiset<int> all_years( year, year + num_cars );
print( unique_years );
print( all_years );
typedef map<string,int> Car_map;
Car_map unique_cars;
transform( name, name+num_cars, year,inserter( unique_cars, unique_cars.end() ),make_pair<string,int> );
Car_map::const_iterator unique_cars_end = unique_cars.end();
for( Car_map::const_iterator i = unique_cars.begin();i != unique_cars_end; ++i )
cout << i->second << " " << i->first << endl;
typedef multimap<string,int> Car_multimap;
Car_multimap all_cars;
transform( name, name+num_cars, year,inserter( all_cars, all_cars.end() ),make_pair<string,int> );
Car_multimap::const_iterator all_cars_end = all_cars.end();
for( Car_multimap::const_iterator i = all_cars.begin();i != all_cars_end; ++i )
cout << i->second << " " << i->first << endl;
}
|