Skip to main content

7.7) Maps


vector<string> words;
...
map<string,int> frequency;
for (int i = 0; i < words.size(); ++i) {
  string word = words[i];
  ++frequency[word];
}
  • Maps are made available via #include <map>
  • Map elements are pairs consisting of a key and a value; the types of key and value must be specified when creating the map
  • [] is used to insert a new key-value pair into the map or look up an existing value by its key; using [] for a key that is not present will insert that key and a default value into the map
  • The find method is used to search a map for a given key; it returns an iterator (see later)

See the page on maps at cppreference.com for further information.