- what are maps
- mathematical behavior - functions
- 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
- the pair datatype
- represents a pair of values
- a template class of two parameters
- the public member fields are
first
and second
- the handy
make_pair()
template function
-
std::pair<string, int> count("hello", 1);
vs.
-
make_pair("hello", 1);
- watch out for the template parameter-type matching
- defined in
<utility>
- maps behavior
- a special kind of set 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
- 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 is a template class
- with template
<class Key>
-
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 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
- growing and shrinking maps
- 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>
- map
insert(i, v)
- inserter compatibility
- the iterator
i
is a hint
- return an iterator to
v
- insertion not indicated for maps
- map
insert(start, end)
- no iterator, returns
nothing
- map
erase(i)
- deletes only the referenced pair
- map
m.erase(k)
- deletes all pairs containing key
k
; returns the number of deleted pairs
- map
erase(s, e)
-
size()
, max_size()
, and empty()
member functions
- map
count(k)
member function - return the number of
pairs having key value k
- map relation operators - equality and lexicographic less-than
- equality - the same number of the same kind of pairs
- iterators
- pairs in a map 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 iterators are bidirectional iterators - no jumping, no
distance measurement, no inequality relations
- map iterators are mutable - 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()
- range member functions - map
- 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
-
find(k)
- return an iterator to pair with key k
or
end()
This page last modified on 26 October 2001.