Initializing Associative Containers : multiset « set multiset « C++ Tutorial






#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;
}








19.10.multiset
19.10.1.Add elements into an integer set and output all added elements
19.10.2.Assign elements to another multiset with ascending order
19.10.3.Initializing Associative Containers
19.10.4.Modify or Remove a Value in a Set or Multiset
19.10.5.Use std::copy to print all elements in a multiset
19.10.6.Define multiset< int, less< int > >