File: mapconstructors.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (58 lines) | stat: -rw-r--r-- 2,734 bytes parent folder | download | duplicates (3)
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
    The following hi(map constructors) constructors are available for the
tt(map) container:
    itemization(
    it() The copy and move constructors are available;
    it() A tt(map) may be constructed empty:
    verb(map<string, int> object;)

Note that the values stored in maps may be containers themselves. For
example, the following defines a tt(map) in which the value is a tt(pair): a
container
 hi(nested container) hi(container: nested) nested under another container:
        verb(    map<string, pair<string, string>> object;)

Note the use of the two
 hi(>>: with templates)hi(angle brackets: consecutive)
 i(consecutive closing angle brackets), which does not result in ambiguities
as their syntactical context differs from their use as binary operators in
expressions.
    it() A map may be initialized using two iterators.  The iterators may
either point to tt(value_type) values for the map to be constructed, or to
plain ti(pair) objects. If pairs are used, their tt(first) element represents
the type of the keys, and their tt(second) element represents the type of the
values. Example:
    verb(pair<string, int> pa[] =
{
    pair<string,int>("one", 1),
    pair<string,int>("two", 2),
    pair<string,int>("three", 3),
};

map<string, int> object(&pa[0], &pa[3]);)

In this example, tt(map<string, int>::value_type) could have been written
instead of tt(pair<string, int>) as well.

    If tt(begin) represents the first iterator that is used to construct a map
and if tt(end) represents the second iterator, rangett(begin, end) will be
used to initialize the map. Maybe contrary to intuition, the tt(map)
constructor only enters em(new) keys.  If the last element of tt(pa) would
have been tt("one", 3), only em(two) elements would have entered the tt(map):
tt("one", 1) and tt("two", 2). The value tt("one", 3) would silently have been
ignored.

    The tt(map) receives its own copies of the data to which the iterators
point as illustrated by the following example:
        verbinclude(-a examples/mapconstruct.cc)

    When tracing the output of this program, we see that, first, the
constructor of a tt(MyClass) object is called to initialize the anonymous
element of the array tt(pairs). This object is then copied into the first
element of the array tt(pairs) by the copy constructor. Next, the original
element is not required anymore and is destroyed. At that point the array
tt(pairs) has been constructed. Thereupon, the tt(map) constructs a temporary
tt(pair) object, which is used to construct the map element. Having
constructed the map element, the temporary tt(pair) object is
destroyed. Eventually, when the program terminates, the tt(pair) element
stored in the tt(map) is destroyed too.
    )