Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
STL container similar to std::map, except that it can hold multiple values for one key.
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.
To add a key-value-pair, use std::multimap<T>::insert. To find a range of values, use std::multimap<T>::equal_range. This method returns a std::pair of iterators.
{
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';
}
}
* Wikipedia's page about std::multimap
* SGI's page about std::multimap