C++中的数组类型是继承了C语言的特性,在使用数组的时候要注意数组越界操作问题。为了更安全的对数组进行操作,C++提出了数组模板类array。

1. array模板类的定义

1.1 array模板类的声明

1
template <class Tsize_t N> class array;

数组类是固定大小的序列容器,它们包含以严格线性序列排序的特定数量的元素。数组类具有固定大小,并且不通过分配器管理其元素的分配,它们是封装固定大小元素数组的聚合类型。

1.2 容器属性

序列容器中的元素按严格的线性顺序排序。各个元素按其顺序访问它们的位置。
元素存储在连续的存储器位置,允许对元素进行恒定时间随机访问。可以偏移元素的指针以访问其他元素。
容器使用隐式构造函数和析构函数静态分配所需的空间。它的大小是编译时常量。没有内存或时间开销。

1.3 array模板类的说明

array模板类中T为包含元素的类型(std::array::value_type)N为元素个数。

1.4 array模板类头文件

使用array模板类之前需要包含#include <array>头文件!

2. array模板类的使用

2.1 Iterators

Iterators迭代器的作用是遍历array数组类中的元素。可以通过begin/end()rbegin/rend()cbegin/cend()、**crbegin/crend()**等函数进行访问。

函数功能
beginReturn iterator to beginning
endReturn iterator to end
rbeginReturn reverse iterator to reverse beginning
rendReturn reverse iterator to reverse end
cbeginReturn const_iterator to beginning
cendReturn const_iterator to end
crbeginReturn const_reverse_iterator to reverse beginning
crendReturn const_reverse_iterator to reverse end

参考代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <array>

int main(void) {
std::array<int, 5> arr = {1, 2, 3, 4, 5};
std::cout << "array values: ";
for (auto it = arr.begin(); it != arr.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}

运行结果如下所示:

1
array values: 1 2 3 4 5

2.2 Capacity

array数组容器的大小是固定的。可以通过sizeof()size()max_size()、**empty()**等函数进行检测。

函数功能
sizeReturn size
max_sizeReturn maximum size
emptyTest whether list is empty

测试array数组容器大小的参考代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <array>

int main(void) {
std::array<int, 5> arr = {1, 2, 3, 4, 5};
std::cout << "sizeof(array) = " << sizeof(arr) << std::endl;
std::cout << "size of array = " << arr.size() << std::endl;
std::cout << "max_size of array = " << arr.max_size() << std::endl;
if (arr.empty()) {
std::cout << "array is empty!" << std::endl;
} else {
std::cout << "array is not empty!" << std::endl;
}
return 0;

}

运行结果如下所示:

1
2
3
4
sizeof(array) = 20
size of array = 5
max_size of array = 5
array is not empty!

2.3 Element access

可以通过下标[ ]at()front()back()data()等函数访问array容器内的元素。

函数功能
operator[ ]Access element
atAccess element
frontAccess first element
backAccess last element
dataGet pointer to first data

参考代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <array>

int main(void) {
std::array<int, 5> arr = {1, 2, 3, 4, 5};
std::cout << "array[0] = " << arr[0] << std::endl;
std::cout << "array.at(4) = " << arr.at(4) << std::endl;
std::cout << "array.front() = " << arr.front() << std::endl;
std::cout << "array.back() = " << arr.back() << std::endl;
std::cout << "&array: " << arr.data() << " = " << &arr << std::endl;
return 0;

}

运行结果如下所示:

1
2
3
4
5
array[0] = 1
array.at(4) = 5
array.front() = 1
array.back() = 5
&array: 0x7ffd22df6e50 = 0x7ffd22df6e50

2.4 Modifiers

可以使用fill()swap()等函数对array容器整体进行操作。

函数功能
fillFill array with value
swapSwap content

参考代码如下所示:

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
#include <iostream>
#include <array>

int main(void) {
std::array<int, 5> arr;
arr.fill(5); // fill
std::cout << "array values: ";
for (auto i : arr) {
std::cout << i << " ";
}
std::cout << std::endl;
std::array<int, 3> first = {1, 2, 3};
std::array<int, 3> second = {6, 5, 4};
std::cout << "first array values: ";
for (auto it = first.begin(); it != first.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
std::cout << "second array values: ";
for (auto it = second.begin(); it != second.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
first.swap(second); // swap
std::cout << "swap array success!" << std::endl;
std::cout << "first array values: ";
for (auto it = first.begin(); it != first.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
std::cout << "second array values: ";
for (auto it = second.begin(); it != second.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}

运行结果如下所示:

1
2
3
4
5
6
array values: 5 5 5 5 5 
first array values: 1 2 3
second array values: 6 5 4
swap array success!
first array values: 6 5 4
second array values: 1 2 3

2.5 Compare

还可以使用**> < ==等符号对两个array**数组容器进行比较。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <array>

int main(void) {
std::array<int,5> a = {10, 20, 30, 40, 50};
std::array<int,5> b = {10, 20, 30, 40, 50};
std::array<int,5> c = {50, 40, 30, 20, 10};
if (a == b) {
std::cout << "a == b" << std::endl;
} else {
std::cout << "a != b" << std::endl;
}
if (a == c) {
std::cout << "a == c" << std::endl;
} else {
std::cout << "a != c" << std::endl;
}
if (a < c) {
std::cout << "a < c" << std::endl;
} else {
std::cout << "a >= c" << std::endl;
}
return 0;
}

运行结果如下所示:

1
2
3
a == b
a != c
a < c

2.6 Other

c++重载了get()函数来访问数组容器中的元素,为了和元组相似,还重载了tuple_sizetuple_element类型。

函数功能
get( array)Get element (tuple interface)
tuple_element<array>Tuple element type for array
tuple_size<array>Tuple size traits for array

参考代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <array>
#include <tuple>

int main(void) {
std::array<int,3> myarray = {10, 20, 30};
std::tuple<int, int, int> mytuple (10, 20, 30);
std::tuple_element<0, decltype(myarray)>::type myelement; // int myelement
myelement = std::get<2>(myarray);
std::get<2>(myarray) = std::get<0>(myarray);
std::get<0>(myarray) = myelement;
std::cout << "first element in myarray: " << std::get<0>(myarray) << std::endl;
std::cout << "first element in mytuple: " << std::get<0>(mytuple) << std::endl;
return 0;
}

运行结果如下所示:

1
2
first element in myarray: 30
first element in mytuple: 10

建议:多使用array数组容器代替c类型数组,使操作数组元素更加安全!