Maps and multimaps

A map container stores two different items, a key and a value, and it maintains the items in an sort order according to the key. A sorted map means that it is quick to locate an item. The class has the same interface as other containers to add items: you can put them into the container via the constructor, or you can use member methods insert and emplace. You also have access to items via iterators. Of course, an iterator gives access to a single value, so with a map this will be to a pair object that has both the key and the value:

    map<string, int> people;     people.emplace("Washington", 1789);     people.emplace("Adams", 1797);     people.emplace("Jefferson", 1801);     people.emplace("Madison", 1809);  people.emplace("Monroe", ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.