Lecture Notes for SE 598, Data Structures and Algorithms
2 August 2000 - STL Maps and Multimaps
- what are maps and multimaps
- mathematical behavior - functions and relations (approximate)
- a special kind of vector (approximate)
- a programming data structure
- usually known as tables or associative arrays
- usually restricted to strings as keys
- a sorted associative container
- sorted - as with the sets and multisets, but only on the first value
- associative - the second value
- maps and multimaps - stl behavior
- a special kind of set or multiset - of value pairs
- the first value is the key, the second is the value associated with
the key
- two types - of the key value map and of the associated value
- the third type is the pair itself
- having a pair of values makes things interesting
- value pairs stored in sorted order on keys only
- maps remove pairs with duplicate keys only; multimaps keep them
- grows and shrinks; don't need to be sized
- writing keys is more restricted than reading keys
- values may be read and written with equal facility
- a map-multimap is a template class
- with template
<class Key class T, class Compare = less<T>, class Allocator = allocator>
-
Key
is the type of the first value stored in the map
-
T
is the type of the values associated with keys
-
Compare
determines sorting order and equality over keys
- tricotomy, equality
-
<
is acceptable, <=
is not
- map-multimap constructors
- the default constructor -
map<Key,T> m(const Compare & c = Compare())
- the copy constructor -
map<Key,T> map1(map2);
- the segment-copy constructor
map<Key,T> t(start, end);
- also
the optional compare parameter
- what kind of values are given in the iterator range
- multimap constructors are the same as the map constructors, except with
multimap
instead of map
- growing and shrinking maps and multimaps
- no
reserve()
, capacity()
, d.push_back(v)
,
d.pop_back()
, d.push_front(v)
, or d.pop_front()
member
functions
-
insert()
and erase()
- map
m.insert(v)
- no iterator
- what is the type of
v
- pair<Key,T>
- returns
pair<iterator,bool>
- multimap
mm.insert(v)
always inserts v
, returns an iterator
to v
- map and multimap
insert(i, v)
- inserter compatibility
- the iterator
i
is a hint
- return an iterator to
v
- insertion not indicated for maps
- map and multimap
insert(start, end)
- no iterator, returns
nothing
- map and multimap
erase(i)
- deletes only the referenced pair
- map and multimap
m.erase(k)
- deletes all pairs containing key
k
; returns the number of deleted pairs
- map and multimap
erase(s, e)
-
size()
, max_size()
, and empty()
member functions
- map and multimap
count(k)
member function - return the number of
pairs having key value k
- map and multimap relation operators - equality and lexicographic
less-than
- equality - the same number of the same kind of pairs
- iterators
- pairs in a map or multimap have type
pair<const Key,T>
- the type
pair<Key,T>
is not the same as the type pair<const
Key,T>
- the iterators reference pairs of type
pair<const Key,T>
- a pair's key can't be changed, but its value can be
- map and multimap iterators are bidirectional iterators - no jumping, no
distance measurement, no inequality relations
- map and multimap iterators are mutable - unlike sets and multimaps; but
the key value is constant
- the usual member functions -
begin()
, end()
, and so on
- reading and writing values
- iterator read and write, but write restricted to values, not keys
- map value access via the
[]
operator
-
m[k]
always returns a value, even if k
isn't in m
as a
key
- if
k
isn't a key in m
, m[k]
returns the default value
T()
- the
[]
operator doesn't apply to multimaps - which value is
returned
- range member functions - map and multimap
- lower_bound(k) - the iterator to the first (or leftmost) pair with a
key of at least
k
or end()
- upper_bound(k) - the iterator to the last (or rightmost) pair with a
key of at most
k
or end()
- equal_range(k) - the pair
(lower_bound(k), upper_bound(k))
- other member functions
- map and multimap
find(k)
- return an iterator to pair with key
k
or end()
- map and multimap
count(k)
- return the number of pairs having key
k
This page last modified on 3 August 2000.