use subscript operator to write value to a map : map insert « map multimap « C++ Tutorial






#include <algorithm>
#include <iostream>
#include <map>
#include <utility>

using namespace std;

int main( )
{
   const char* word[] = { "A", "B", "C", "D","E" };
   const char* clue[] = { "a", "b","c", "d","e" };

   map<string,string> dictionary1;
   transform( word, word+sizeof(word)/sizeof(word[0]), clue,inserter( dictionary1, dictionary1.end() ),make_pair<string,string> );
   cout << "There are " << dictionary1.size() << " words in the dictionary\n\n";

   cout << "The clue for POPS is \"" << dictionary1["POPS"] << "\"\n"
      << "The clue for TAT is \"" << dictionary1["TAT"] << "\"\n"
      << "The clue for ESPO0 is \"" << dictionary1["ESPO0"] << "\"\n";

   cout << "\nThere are " << dictionary1.size() << " words in the dictionary\n\n";

   // use subscript operator to write value
   dictionary1["ESPO0"] = "Typo in name";
   cout << "The clue for ESPO0 is \"" << dictionary1["ESPO0"] << "\"";

 
}








23.6.map insert
23.6.1.Insert characters into map.
23.6.2.Use insert() to add an entry
23.6.3.Duplicate keys are not allowed in a map
23.6.4.Insert a pair using function make_pair
23.6.5.Insert a pair object directly
23.6.6.Insert using an array-like syntax for inserting key-value pairs
23.6.7.Insert key-value pairs into the map using value_type
23.6.8.use subscript operator to write value to a map
23.6.9.Insert value pair to map