目录
1、array模板类的定义
(1)array模板类的声明
(2)容器属性
(3)array模板类的说明
(4)array模板类头文件
2、array模板类的使用
(1)Iterators
(2)Capacity
(3)Element access
(4)Modifiers
(5)Compare
(6)Other
本章是对c++ array模板类的知识归纳,讲述了c++中array模板类的使用,不涉及原理方面的内容。
c++中的数组类型是继承了c语言的特性,在使用数组的时候要注意数组越界操作问题。为了更安全的对数组进行操作,c++提出了数组模板类array。
template class array;
数组类是固定大小的序列容器,它们包含以严格线性序列排序的特定数量的元素。数组类具有固定大小,并且不通过分配器管理其元素的分配,它们是封装固定大小元素数组的聚合类型。
array模板类中T为包含元素的类型(std::array::value_type),N为元素个数。
使用array模板类之前需要包含#include
Iterators迭代器的作用是遍历array数组类中的元素。可以通过begin/end()、rbegin/rend()、cbegin/cend()、crbegin/crend()等函数进行访问。
begin | Return iterator to beginning |
end | Return iterator to end |
rbegin | Return reverse iterator to reverse beginning |
rend | Return reverse iterator to reverse end |
cbegin | Return const_iterator to beginning |
cend | Return const_iterator to end |
crbegin | Return const_reverse_iterator to reverse beginning |
crend | Return const_reverse_iterator to reverse end |
参考代码如下所示:
/*****************************************************
Copyright (C) 2018. All rights reserved.
File name : array.cpp
Version : v1.0
Author : zhengqijun
Date : 2018-08-10
Function List :
Description : array container.
******************************************************/
#include
#include
int main(void) {
std::array 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;
}
运行结果如下所示:
array values: 1 2 3 4 5
array数组容器的大小是固定的。可以通过sizeof()、size()、max_size()、empty()等函数进行检测。
size | Return size |
max_size | Return maximum size |
empty | Test whether list is empty |
测试array数组容器大小的参考代码如下所示:
/*****************************************************
Copyright (C) 2018. All rights reserved.
File name : array.cpp
Version : v1.0
Author : zhengqijun
Date : 2018-08-10
Function List :
Description : array container.
******************************************************/
#include
#include
int main(void) {
std::array 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;
}
运行结果如下所示:
sizeof(array) = 20
size of array = 5
max_size of array = 5
array is not empty!
可以通过下标[ ]、at()、front()、back()、data()等函数访问array容器内的元素。
operator[ ] | Access element |
at | Access element |
front | Access first element |
back | Access last element |
data | Get pointer to first data |
参考代码如下:
/*****************************************************
Copyright (C) 2018. All rights reserved.
File name : array.cpp
Version : v1.0
Author : zhengqijun
Date : 2018-08-10
Function List :
Description : array container.
******************************************************/
#include
#include
int main(void) {
std::array 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;
}
运行结果如下所示:
array[0] = 1
array.at(4) = 5
array.front() = 1
array.back() = 5
&array: 0x7ffd22df6e50 = 0x7ffd22df6e50
可以使用fill()、swap()等函数对array容器整体进行操作。
fill | Fill array with value |
swap | Swap content |
参考代码如下所示:
/*****************************************************
Copyright (C) 2018. All rights reserved.
File name : array.cpp
Version : v1.0
Author : zhengqijun
Date : 2018-08-10
Function List :
Description : array container.
******************************************************/
#include
#include
int main(void) {
std::array arr;
arr.fill(5); // fill
std::cout << "array values: ";
for (auto i : arr) {
std::cout << i << " ";
}
std::cout << std::endl;
std::array first = {1, 2, 3};
std::array 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;
}
运行结果如下所示:
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
还可以使用> < ==等符号对两个array数组容器进行比较。
/*****************************************************
Copyright (C) 2018. All rights reserved.
File name : array.cpp
Version : v1.0
Author : zhengqijun
Date : 2018-08-10
Function List :
Description : array container.
******************************************************/
#include
#include
int main(void) {
std::array a = {10, 20, 30, 40, 50};
std::array b = {10, 20, 30, 40, 50};
std::array 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;
}
运行结果如下所示:
a == b
a != c
a < c
c++重载了get()函数来访问数组容器中的元素,为了和元组相似,还重载了tuple_size和tuple_element类型。
get( array) | Get element (tuple interface) |
tuple_element |
Tuple element type for array |
tuple_size |
Tuple size traits for array |
参考代码如下所示:
/*****************************************************
Copyright (C) 2018. All rights reserved.
File name : array.cpp
Version : v1.0
Author : zhengqijun
Date : 2018-08-10
Function List :
Description : array container.
******************************************************/
#include
#include
#include
int main(void) {
std::array myarray = {10, 20, 30};
std::tuple 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;
}
运行结果如下所示:
first element in myarray: 30
first element in mytuple: 10
建议:多使用array数组容器代替c类型数组,使操作数组元素更加安全!