File: pair.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 (77 lines) | stat: -rw-r--r-- 3,952 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
The hi(pair container) ti(pair) container is a rather basic container. It is
used to store two elements, called ti(first) and ti(second), and that's about
it. Before using tt(pair) containers the header file tthi(utility) must be
included.

    The tt(pair)'s data types are specified when the tt(pair) object is
defined (or declared) using the template's angle bracket notation (cf. chapter
ref(TEMPLATES)). Examples:
        verb(    pair<string, string> piper("PA28", "PH-ANI");
    pair<string, string> cessna("C172", "PH-ANG");)

here, the variables tt(piper) and tt(cessna) are defined as tt(pair)
variables containing two tt(strings). Both strings can be retrieved using the
tt(first) and tt(second) fields of the tt(pair) type:
        verb(    cout << piper.first << '\n' <<      // shows 'PA28'
            cessna.second << '\n';      // shows 'PH-ANG')

The tt(first) and tt(second) members can also be used to reassign values:
        verb(    cessna.first = "C152";
    cessna.second = "PH-ANW";)

If a tt(pair) object must be completely reassigned, an em(anonymous)
hi(anonymous pair) pair object can be used as the i(right-hand) operand of
the assignment. An anonymous variable defines a temporary variable (which
receives no name) solely for the purpose of (re)assigning another variable of
the same type. Its hi(anonymous variable: generic form) generic form is
        verb(    type(initializer list))

Note that when a tt(pair) object is used the type specification is not
completed by just mentioning the containername tt(pair). It also requires the
specification of the data types which are stored within the pair. For this the
(template) i(angle bracket notation) is used again. E.g., the reassignment
of the tt(cessna) pair variable could have been accomplished as follows:
        verb(    cessna = pair<string, string>("C152", "PH-ANW");)

In cases like these, the tt(type) specification can become quite
elaborate, in which case tt(using) declarations can be used to improve
readability. If many ti(pair<type1, type2>) clauses are
used in a source, the typing effort may be reduced and readability might be
improved by first defining a name for the clause, and then using the defined
name later. E.g.,
        verb(    using pairStrStr = pair<string, string>;

    cessna = pairStrStr("C152", "PH-ANW");)

All abstract containers are class templates, and the
types for which class templates are initialized are commonly specified between
pointed brackets following the class template's name. However, the compiler
may be able to 
    hi(class templates: deducing parameters) deduce the container's types from
the types of arguments that are specified when constructing the
container. E.g., when defining
        verb(    pair values{ 1, 1.5 };)

the compiler deduces that tt(values.first) is an tt(int) and
tt(values.second) is a tt(double). Sometimes the class template's types cannot
be deduced. In those cases the intended types must explicitly be specified:
        verb(    pair<int, double> values;)

Although the compiler will deduce types whenever it can, it might not
deduce the types we had in mind. Had we defined
        verb(    pair cessna{ "C172", "PH-BVL" };)

then the compilation would succeed, but an expression  like 
        tt(cout << cessna.first.length()) would not compile, as tt("C172") is
a NTBS, and hence tt(cessna.first) is a tt(char *). In this case simply
appending an tt(s) to the NTBSs fixes the problem, but such a simple fix might
not always be available. Section ref(VECTOR) has contains more information
about deducing template parameter types.

    Apart from this (and the basic set of operations (assignment and
comparisons)) the tt(pair) offers no further i(functionality). It is, however,
a basic ingredient of the upcoming abstract containers tt(map, multimap) and
tt(hash_map).

bf(C++) also offers a emi(generalized pair) container: the em(tuple), covered
in section ref(TUPLES).