// ========================================================================== // $Id: stl_unorderedmap.cpp,v 1.1 2014/11/01 05:04:47 jlang Exp $ // CSI2372 example Code for lecture 11 // ========================================================================== // (C)opyright: // // Jochen Lang // SITE, University of Ottawa // 800 King Edward Ave. // Ottawa, On., K1N 6N5 // Canada. // http://www.site.uottawa.ca // // Creator: jlang (Jochen Lang) // Email: jlang@site.uottawa.ca // ========================================================================== // $Log: stl_unorderedmap.cpp,v $ // Revision 1.1 2014/11/01 05:04:47 jlang // Added uordered map examples // // ========================================================================== #include #include #include #include #include using std::cout; using std::endl; using std::string; using std::map; using std::multimap; using std::unordered_map; using std::unordered_multimap; int main( int argc, char* argv[] ) { // multimap with string as a key and int as value multimap tmMap {{"Smith, John",31245},{"Doe, Jane",1}, {"Scott, Stephen",34411},{"Sobey, Anna",89554},{"Doe, Jane",245876}, {"Doe, Jane", 2}}; // duplicate key - fine for multimap, no insertion for map cout << "Content of tmMap: " << endl; for ( auto tPair : tmMap ) { cout << "Key: " << std::setw(18) << tPair.first << " "; cout << "Value: " << tPair.second << endl; } cout << "--------------------------------------" << endl; // copy pairs into a hash map - types must match unordered_multimap hmMap(tmMap.cbegin(),tmMap.cend()); cout << "Content of hmMap: " << endl; for ( auto hPair : hmMap ) { cout << "Key: " << std::setw(18) << hPair.first << " "; cout << "Value: " << hPair.second << endl; } cout << "--------------------------------------" << endl; // copy pairs into a hash map - without multiples unordered_map hMap(hmMap.cbegin(),hmMap.cend()); cout << "Content of hMap: " << endl; for ( auto hPair : hMap ) { cout << "Key: " << std::setw(18) << hPair.first << " "; cout << "Value: " << hPair.second << endl; } return 0; }