Facebook Twitter

Associative containers (C++) Design[edit] Characteristics[edit] Key uniqueness: in map and set each key must be unique. multimap and multiset do not have this restriction.Element composition: in map and multimap each element is composed from a key and a mapped value.

Associative containers (C++)

C++: STL Map and MultiMap. Sparse array example: (why hold space for thousands of elements when all we have is five) Compile: g++ testMap.cpp Run: .

C++: STL Map and MultiMap

/a.out Employees[3374]=Charlie M. STL Tutorial - Map Class. Suppose that you're working with some data that has values associated with strings -- for instance, you might have student usernames and you want to assign them grades.

STL Tutorial - Map Class

How would you go about storing this in C++? One option would be to write your own hash table. Map. Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.


In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in member type value_type, which is a pair type combining both: Internally, the elements in a map are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare). map containers are generally slower than unordered_map containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order. The mapped values in a map can be accessed directly by their corresponding key using the bracket operator ((operator[]).

Map::insert. Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted.


Because element keys in a map are unique, the insertion operation checks whether each inserted element has a key equivalent to the one of an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value). For a similar container allowing for duplicate elements, see multimap.