Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) std::multimap

 

std::multimap is an STL container similar to std::map, except that it can hold multiple values for one key.

 

#include <map>

std::multimap<std::string, int> phonebook;

 

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

 

One key can only have no, one or many values, like a person can have no, one or many phone numbers.

 

 

 

 

 

Example

 

To add a key-value-pair, use std::multimap<T>::insert. To find a range of values, use std::multimap<T>::equal_range. This member function returns a std::pair of iterators.

 

#include <iostream>
#include <map>
#include <string>

int main()
{
  std::multimap<std::string, std::string> questions;

  questions.insert(std::make_pair("A prime number between 10 to 20","11"));
  questions.insert(std::make_pair("A prime number between 10 to 20","13"));
  questions.insert(std::make_pair("A prime number between 10 to 20","17"));
  questions.insert(std::make_pair("A prime number between 10 to 20","19"));

  typedef std::multimap<std::string, std::string>::const_iterator Iterator;

  const std::pair<Iterator,Iterator> answers
    = questions.equal_range("A prime number between 10 to 20");

  for (Iterator i = answers.first; i!= answers.second; ++i)
  {
    std::cout << i->second << '\n';
  }
}

 

 

 

 

 

External links

 

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml