// @topic T53335 12/11/2012 -- STL containers and iterators // @brief std::map, map iterator, std::pair, std::make_pair( ), auto #include <string> #include <iostream> #include <vector> #include <sstream> #include <map> int main() { std::map< std::string, int > mapping; int value = 0; std::string key; for(;;) { ++value; std::cout << "key for value " << value << " (or Q to stop): "; std::cin >> key; if ( key == "Q" ) break; // to populate the map //std::pair< std::string, int > map_entry( key, value ); //mapping.insert( map_entry ); mapping.insert( std::make_pair( key, value ) ); } for(;;) { std::cout << "enter key (or Q to stop): "; std::cin >> key; if ( key == "Q" ) break; if ( mapping.count( key ) == 0 ) { std::cout << "the key is invalid\n"; continue; } value = mapping[ key ]; std::cout << key << "->" << value << "\n"; } auto it_start = mapping.begin(); auto it_end = mapping.end(); for ( ; it_start != it_end; ++it_start ) { //std::pair< std::string, int > map_entry = *it_start; //std::cout << map_entry.first << "->" << map_entry.second << "\n"; std::cout << it_start->first << "->" << it_start->second << "\n"; } }