October 1999
Beginner
304 pages
7h 11m
English
A map is defined as a pair of values: a key, typically a string that serves as an index and a value associated with that key. A dictionary is one example of a map. A program analyzing the occurrence count of words in a text keeps a map with a string key and an integer value representing an occurrence count:
#include <map> #include <string> map<string,int> words;
The simplest way to enter a key/value pair is
words[ "vermeer" ] = 1;
For our word occurrence program, we can write the following:
string tword;
while ( cin >> tword )
words[tword]++; The expression
words[tword]
retrieves the value associated with the string tword contains. If tword is not present in the map, it is entered into the map with a default value of ...
Read now
Unlock full access