-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24_maps.html
61 lines (50 loc) · 2.4 KB
/
24_maps.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!-- cribtutor-stl by NewForester is licensed under a Creative Commons Attribution-ShareAlike 4.0 International Licence. -->
<h1> STL Associative Containers </h1>
<h2> The map and multimap Containers </h2>
<p>
map<> and multimap<> are associative containers
in which '[<em>key</em>,<em>value</em>]' pair<> structures are stored in <em>ascending</em> order by key.
</p><p>
<em>multimap</em><> allows duplicate keys, <em>map</em><> does not.
Keys are <em>immutable</em>.
</p>
<p>
Search and (in general) insertion operations have <em>logarithmic</em> complexity.
Erase by iterator has <em>constant</em> complexity.
</p><p>
Iterators are <em>bi-directional</em> but <em>map</em><> supports random access.
Insert and erase operations do not <em>invalidate</em> iterators.
</p><p>
Iterators point to '[key,value]' <em>pair</em><> structures so compatibility within iterators for other types of containers is limited.
This affects constructors and insert operations that takes an iterator <em>range</em>.
</p>
<p>
To use map<> and multimap<> containers include the <<em>map</em>> header file.
</p>
<p>
For map<> and multimap<>, the template definitions are:
<pre>
template< typename Key, typename T, class Compare=less<Key>, class Allocator=allocator<pair<const Key, T> > > class map;
template< typename Key, typename T, class Compare=less<Key>, class Allocator=allocator<pair<const Key, T> > > class multimap;
</pre>
</p><p>
The typename Key is the type of the <em>key</em> and typename T the type of the <em>value-objects</em> stored in the container.
They need not be the same type.
</p><p>
The class Compare defines the <em>comparator</em> used to order objects within the container.
It is an advanced option.
User-defined classes must implement the pertinent operator (the default is <em>operator<</em>).
</p><p>
The class Allocator defines the <em>allocator</em> used when copying objects into the container.
It is a very advanced option.
The default uses global new() and delete().
</p><p>
Note, from the declaration of the allocator, what is stored in a map container
is a <em>pair</em><>.
</p><p>
There is a useful value_type defined:
<pre>
typedef pair<const Key, T> value_type;
</pre>
The <em>first</em> is the immutable key and the <em>second</em> is the value-object.
</p>