liveBook · Manning

Std::multimap

As explained in Learn C++ by Example

std::multimap is a C++ standard library associative container that stores key-value pairs while allowing multiple elements with the same key.

std::multimap is an ordered associative container for key-value pairs that permits several values under one key. It suits dictionaries where a word can have multiple definitions. section 7.2 section 7.4

Include <map> and insert pairs with insert or construct them in place with emplace. Unlike std::map, a multimap has no operator[] or at function, so retrieval uses lower_bound and upper_bound, or equal_range, to obtain an iterator range containing every matching value. section 7.2.3

std::multimap<int, std::string> mm{
   {1,"a"s}, {1,"a"s}, {3,"c"s}, {3,"g"s}, {5,"e"s}, {5,"h"s},
};

A small ordered multimap can contain duplicate keys and several different values, making the lookup range visible in a concrete initializer. section 7.2.3

For a key with matches, equal_range returns the first element not less than that key and the first element greater than it. Walk from the lower iterator to the upper iterator to process all associated pairs. The elements remain in tree nodes, and ordered associative-container operations have logarithmic complexity. section 7.2.3 section 7.4

Loading a dictionary

Read a comma-separated file line by line, split each valid line into a word and definition, normalize the key to lowercase, and emplace the pair into a std::multimap<std::string, std::string>. A lookup can then return every definition for the word. The same range technique supports the answer-smash game: find words beginning with a stem, then select the first, last, or a random matching entry for testing or play. section 7.3.1 section 7.3.3

How Learn C++ by Example covers it

How deep it goes
Explains insertion, range lookup, tree nodes, and file loading, but defers broader implementation details beyond selected behavior.
Examples
Uses duplicate-key integer pairs, a word-definition dictionary, comma-separated files, and an answer-smash word-selection game.
What it assumes
Assumes familiarity with std::map, pairs, iterators, strings, and basic file streams while reinforcing ordered-range lookup.
What you can do after
Builds dictionaries, retrieves all values for a key, loads entries from files, and selects deterministic or random candidates from lookup ranges.
How it connects
Connects multimap with associative containers, std::map, iterators, equal_range, file streams, lambdas, and random sampling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.