1 is associated with "eins".
translate(1) = "eins".
template <
typename FirstT, typename SecondT >
struct pair {
FirstT first;
SecondT second;
pair(FirstT f, SecondT s)
: first(f), second(s) { }
};
make_pair() template function.
std::pair<string, int> count("hello", 1); vs.
make_pair("hello", 1);
<utility>, but usually included as part of other
includes (e.g., <map>).
std::map<keyT, avalueT>
std::pair<const keyT, avalueT>
std::map<keyT,avalueT>::value_type.
const later.
keyT, also known as std::map<keyT,avalueT>::key_type.
[ (1, 10) (2, 9) (3, 8) ] is a map.
[ (10, 1) (11, 2) (8, 3) ] is a not a map (probably).
(1, "one") to [ (1, "eins") ] and get[ (1, "eins") ].
(1, "one") to [ (i, "one") ] and get[ (1, "one") (i, "one") ].
const KeyT type in the map-element type.
<typename keyT, typename avalueT, typename Compare = less<keyT> >
Compare is part of the map type.
map<int,int,less<int> > is not the same type as
map<int,int,greater<int> >
keyT is the type of the first value stored in the map.
avalueT is the type of the values associated with keys.
Compare determines sorting order and equality over keys.
Compare is a trichotomy.
a < b or b < a or a == b is always
true.
< is a trichotomy, <= is not.
std::map<keyT, avalueT> m(const Compare & c = Compare());
std::map<keyT, avalueT> m1(m2);
std::map<keyT, avalueT> m(start, end);
reserve(), capacity(), push_back(v),
pop_back(), push_front(v), or pop_front() member
functions.
insert() and erase().
m.insert(v) uses no iterator.
v? pair<keyT,avalueT>.
(iterator, bool).
bool is true if the map size increased.
iterator points to the new value if bool is true.
iterator is undefined if bool is false.
m.insert(i, v) provided for compatibility.
i is a hint.
v; insertion not indicated.
m.insert(start, end) returns nothing.
m.erase(i) erases only the referenced pair *i.
m.erase(k) erases all pairs containing key k; returns the
number of erased pairs.
m.erase(s, e) erases the pairs specified in the iterator range.
for (std::map<int, int>::iterator i = iim.begin();i != iim.end();i++)iim.erase(i);
Use iim.clear() instead.
==) and lexicographic (<) relations.
[ (1, "red") (2, "blue") ] != [ (1, 2) (2, 4) ]
p1 < p2
if pa.first < p2.first
or p1.first == p2.first && p1.second < p2.second
pair<const keyT,avalueT>.
<keyT, avalueT> is not the same as the type
<const keyT, avalueT>.
pair<const keyT,avalueT>.
std::map<int, int>::iterator i = iim.begin();*i = std::make_pair(1, 2)i->first = 1i->second = 2
m.begin(), m.end() and so on.
[] operator.
m[k] always returns a map element, even if k isn't in
m as a key.
k isn't a key in m, m[k] returns the default value
avalueT().
(k, avalueT()) to m.
[] because it can change the map.
Careless use of [] can result in gigantic maps.
m.lower_bound(k) returns an iterator to the first (or leftmost)
pair with a key of at least k or end().
m.upper_bound(k) returns an iterator to the first (or leftmost)
pair with a key more than k or end().
m.equal_range(k) returns the pair (m.lower_bound(k),
m.upper_bound(k)).
m.find(k) returns an iterator to pair with key k or
m.end().
m.count(k) returns the number of pairs having key value k.
m.count(k) or m.find() to check for k in m.
if (m[k] == "hi") . . .if (m.find(k) != m.end()) and (m[k] == "hi") if (m.count(k) == 1) and (m[k] == "hi")
m.size(), m.max_size(), and m.empty().
m.empty() to m.size() == 0.
m,
m.insert(p) takes 0(log n).
m.insert(i, p) might take (approximately) constant time if
i is a good guess.
m.erase(i) takes 0(log n).
m[k] takes 0(log n).
m.count(k) and m.find(k) take 0(log n).
m.lower_bound(k), m.upper_bound(k), and
m.equal_range(k), all take 0(log n).
++ and -- are constant time.
m[1] and m[1000000] takes two elements.
[].
This page last modified on 22 April 2003.