1
The ti(array) class implements a
2
hi(array: fixed-size) i(fixed-size array). Before using the tt(array)
3
container the tthi(array) header file must have been included.
5
To define a tt(std::array) both the data type of its elements and its size
6
must be specified: the data type is given after an opening angular bracket,
7
immediately following the `tt(array)' container name. The array's size is
8
provided after the data type specification. Finally, a closing angular bracket
9
completes the array's type. Specifications like this are common practice with
10
containers. The combination of tt(array), type and size defines a
11
em(type). As a result, tt(array<string, 4>) defines another type than
12
tt(array<string, 5>), and a function explicitly defining an tt(array<Type, N>)
13
parameter will not accept an tt(array<Type, M>) argument if tt(N) and tt(M)
16
The array's size may may be defined as 0 (although such an array probably has
17
little use as it cannot store any element). The elements of an array are
18
stored contiguously. If tt(array<Type, N> arr) has been defined, then tt(&a[n]
19
+ m == &a[n + m), assuming tt(0 <= n < N) and assuming tt(0 <= n + m < N).
21
The following constructors, operators, and member functions are available:
23
it() hi(array constructors) Constructors:
25
it() A tt(array) may be constructed with a fixed number tt(N) of
28
array<string, N> object;
30
it() An initial subset of the elements of an array may be
31
initialized using a brace delimited initializer list:
33
array<double, 4> dArr = {1.2, 2.4};
35
Here tt(dArr) is defined as an array of 4 element, with tt(dArr[0]) and
36
tt(dArr[1]) initialized to, respectively 1.2 and 2.4, and tt(dArr[2]) and
37
tt(dArr[3]) initialized to 0. A attractive characteristic of arrays (and
38
other containers) is that containers initialize hi(containers: initialization)
39
their data elements to the data type's default value. The data type's
40
em(default constructor) is used for this i(initialization). With non-class
41
data types the value 0 is used. So, for an tt(array<double, 4>) array we know
42
that all but its explicitly initialized elements are initialized to
44
it() A array may be initialized using a copy constructor:
46
extern array<string, 5> container;
47
array<string, 5> object(container);
50
it() In addition to the standard operators for containers, the tt(array)
51
supports the i(index operator), which can be used to retrieve or reassign
52
individual elements of the array. Note that the elements which are indexed
53
must exist. For example, having defined an empty array a statement like
54
tt(iarr[0] = 18) produces an error, as the array is empty. Note that
55
tt(operator[]) does em(not) respect its
56
i(array bounds). If you want run-time array bound checking, use the array's
58
it() The tt(array) class offers the following
59
hi(array: member functions) member functions:
61
ithtq(at)(Type &at(size_t idx))
62
(returns a reference to the array's element at index position
63
tt(idx). If tt(idx) exceeds the array's size a
64
tt(std::out_of_range) exception is thrown.)
65
ithtq(back)(Type &back())
66
(returns a reference to the last element in the array. It is the
67
i(responsibility of the programmer) to use the member only if the
69
ithtq(begin)(array::iterator begin())
70
(returns an i(iterator) pointing to the first
71
element in the array, returning tt(end) if the array is empty.)
72
ithtq(cbegin)(array::const_iterator cbegin())
73
(returns a i(const_iterator) pointing to the first element in the
74
array, returning tt(cend) if the array is empty.)
75
ithtq(cend)(array::const_iterator cend())
76
(returns a i(const_iterator) pointing just beyond the array's last
78
ithtq(crbegin)(array::const_reverse_iterator crbegin())
79
(returns a i(const_reverse_iterator) pointing to the last element
80
in the array, returning tt(crend) if the array is empty.)
81
ithtq(crend)(array::const_reverse_iterator crend())
82
(returns a i(const_reverse_iterator) pointing just before the
83
array's first element.)
84
ithtq(data)(value_type *data())
85
(returns a pointer to the array's first data element. With a const
86
array a tt(value_type const *) is returned.)
87
ithtq(empty)(bool empty())
88
(returns tt(true) if the array contains no elements.)
89
ithtq(end)(array::iterator end())
90
(returns an iterator pointing beyond the last element in the
92
ithtq(fill)(void fill(Type const &item))
93
(fills all the array's elements with a copy of tt(item))
94
ithtq(front)(Type &front())
95
(returns a reference to the first element in the array. It is the
96
responsibility of the programmer to use the member only if
97
the array is not empty.)
98
ithtq(rbegin)(array::reverse_iterator rbegin())
99
(hi(reverse_iterator) this member returns an iterator pointing to
100
the last element in the array.)
101
ithtq(rend)(array::reverse_iterator rend())
102
(returns an iterator pointing before the first element in the
104
ithtq(size)(constexpr size_t size())
105
(returns the number of elements the array contains.)
106
ithtq(swap)(void swap(<array<Type, N> &other))
107
(swaps the contents of the current and other array. The array
108
other's data type and size must be equal to the data type and size
109
of the object calling tt(swap).)
112
Using an tt(array) rather than a standard bf(C) style array offers several
115
it() All its elements are immediately initialized;
116
it() Introspection is possible (e.g., tt(size) can be used);
117
it() The tt(array) container can be used in the context of templates,
118
there code is developed that operates on data types that become
119
available only after the code itself has been developed;
120
it() Since tt(array) supports reverse iterators, it can be immediately be
121
used with generic algorithms performing `reversed' operations (e.g.,
122
to perform a descending rather than ascending sort (cf. section
125
In general, when looking for a sequential data structure, the tt(array) or
126
tt(vector) (introduced in the next section) should be your `weapon of
127
choice'. Only if these containers demonstrably do not fit the problem at hand
128
you should use another type of container.