Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::map

 

std::map is an STL container for mapping two kinds of data types.

 

#include <iostream>
#include <map>

int main()
{
  std::map<std::string, int> phonebook;
  phonebook["Bilderbikkel"] = 1234567890; //Store
  std::cout << phonebook["Bilderbikkel"] << std::endl; //Read
}

 

The std::string is the key (in this case a last name) and the int is termed the value (in this calse the persons telephone number).

 

One key can only have one value. If a key needs to have one or more values, use a std::multimap.

 

 

 

 

 

std::map code snippets

 

Note that among these are also more general container code snippets.

 

  1. IsKeyInMap, checks if a key is in a std::map
  2. GetKeyWithMaxValue, get the key with the highest value
  3. GetValueInMap, get a value in a std::map
  4. Check if a key is in a std::map, IsKeyInMap
  5. Get a value in a std::map, GetValueInMap
  6. LoopReader, reading a container looped
  7. Reading a container looped, LoopReader

 

 

 

 

 

External links

 

  1. SGI page about std::map

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict