Associative Containers’ Associated Types
• Associative containers declare additional types
– A key_type gives the type of keys the container holds
– A value_type gives the type of values the container holds
– A mapped_type gives type associated with key (maps only)
• Examples for sets (key and value types are the same)
– Associated type set<int>::key_type is int, as is
set<int>::value_type
• Examples for maps (note key becomes const in value)
– Associated type map<int, string>::key_type is int
– Type map<int, string>::mapped_type is string
– Associated type map<int, string>::value_type is
pair<const int, string>
CSE 332: C++ Associative Containers II
Iteration Through Associative Containers
C set
B
A
iter1
D
C multiset
B
A
C 2
iter2
A 3
D 7
C 2
B 2
D
C
B 2
A 3
map
multimap
D 7
C 5
• Iteration is bi-directional
– Can increment (e.g., ++iter1) or decrement (e.g., --iter1) an iterator
• Dereferencing gives read-only access to keys
– E.g., *iter1 = “D”; or iter2->first = “D”; are not allowed
• But, can gain read-write access to mapped value
– E.g., iter2->second = 7; is allowed
CSE 332: C++ Associative Containers II
Adding Elements to a Map or Set
E set (before)
D 2
D
B
B 2
D set (after)
B
A 3
C
E 7
D 2
B 2
E
A 3
map (before)
map (after)
E 7
C 5
• Insert returns a pair (may also rebalance the tree)
– First part is an iterator to element, second is bool (true on success)
– Multimap or multiset insert just returns an iterator to inserted element
• For maps, insert takes a pair (several possible ways)
–
–
–
–
E.g., m.insert({“C”, 5}); if compiler supports list initialization
E.g., m.insert(make_pair(“C”, 5));
E.g., m.insert(pair<string, int>(“C”, 5));
E.g., m.insert(map<string, int>::value_type(“C”, 5));
CSE 332: C++ Associative Containers II
Finding Elements in a Multimap or Multiset
iter2
iter1
B
A
iter2
C
C
B
iter1
A 3
C 2
B 2
C 7
B 5
• Find returns an iterator to first matching element
– Forward iteration from first matching element (if one was found) gives all
other equal elements (find returns past the end iterator if no match)
• Can obtain iterators bounding range of equal elements
– E.g., s.lower_bound(“B”); gives first element not less than “B”
– E.g., s.upper_bound(“B”); gives first element greater than “B”
– E.g., s.equal_range(“B”); returns a pair of iterators bounding the
range of elements with key “B” (or insertion point if match not found)
CSE 332: C++ Associative Containers II
Unordered Containers (UCs)
unordered_set
A
B
unordered_multiset
C
A
B
C
C
unordered_map
A 0
B 7
unordered_multimap
C 2
A 0
B 7
C 3
C 2
• UCs use == to compare elements instead of < to order them
– Types in unordered containers must be equality comparable
– When you write your own structs, overload == as well as <
• UCs store elements in indexed buckets instead of in a tree
– Useful for types that don’t have an obvious ordering relation over their values
• UCs use hash functions to put and find elements in buckets
– May improve performance in some cases (if performance profiling suggests so)
– Declare UCs with pluggable hash functions via callable objects, decltype, etc.
CSE 332: C++ Associative Containers II
Concluding Remarks
• Choose carefully which operators you use
– E.g., , m.insert(map<string, int>::value_type(“C”, 5));
avoids construct/destruct/assign of a temporary (vs. m[“C”] = 5);)
• Also realize that overloaded operator[] has
different interpretations in different containers
– In a vector or other random access sequence container it’s a
constant time indexing operation to a particular location
– In a map or other associative container it’s a logarithmic time
operation (“find” or “insert” versus “read” or “write”)
• Unordered containers give another mix of operations
– E.g., via hashing, for which callable objects can be used
CSE 332: C++ Associative Containers II
© Copyright 2026 Paperzz