格式
template < class T, size_t N > class array;
描述
数组是固定大小的序列容器:它们包含以严格线性顺序排列的特定数量的元素。在内部,数组不保留除其包含的元素之外的任何数据(甚至它的大小,这是一个模板参数,在编译时固定)它在存储大小方面与使用语言括号语法声明的普通数组一样高效。这个类只是为它添加了一层成员函数和全局函数,因此数组可以用作标准容器。与其他标准容器不同,数组具有固定大小,并且不通过分配器管理其元素的分配。它们是封装固定大小元素数组的聚合类型。 因此,它们不能动态扩展或收缩。零大小的数组是有效的,但不应取消引用它们。
特点
- 元素线性顺序排序
- 连续存储
- 固定大小
迭代器(iterator)
代码:
#include<iostream>
#include<array>
using namespace std;
int main(){
array<int, 5> my_array= {1,45,63,21,78};
std::array<int,5>::iterator it;//迭代器
for(it = my_array.begin(); it != my_array.end(); ++it){
cout << *it << endl;
}
std::array<int,5>::reverse_iterator r_it;//反向迭代器
for(r_it = my_array.rbegin(); r_it != my_array.rend(); ++r_it){
cout << *r_it << endl;///输出的结果为倒序,{78,21,63,45,1}
}
std::array<int,5>::const_iterator c_it;//常量(const)迭代器
for(c_it = my_array.cbegin(); c_it != my_array.cend(); ++c_it){
//*c_it = 2;*c_it为常量const无法改变
cout << *c_it << endl;
}
std::array<int,5>::const_reverse_iterator c_r_it;//常量反向迭代器
for(c_r_it = my_array.crbegin(); c_r_it != my_array.rend(); ++c_r_it){
cout << *c_r_it << endl;///输出的结果为倒序,{78,21,63,45,1}
}
return 0;
}
方法(capacity)
代码:
#include<iostream>
#include<array>
using namespace std;
int main(){
array<int, 5> my_array= {1,45,63,21,78};
cout << "数组的元素个数" << my_array.size() << endl;//返回数组的元素个数
cout << "数组的最大元素个数" << my_array.max_size() << endl;//由于array的大小固定,因此size()、max_size()大小一致
cout << (my_array.empty() ? "数组为空" : "数组不为空") << endl;
return 0;
}
结果:
数组的元素个数5
数组的最大元素个数5
数组不为空
Element access
代码:
#include<iostream>
#include<array>
using namespace std;
int main(){
array<int, 3> my_array;
my_array[0] = 1;
my_array.at(1) = 10;
my_array.at(2) = 13;
cout << my_array.at(0) << endl;//对array元素的操作
cout << my_array[1] << endl;//重载[],类似于c++的内嵌数组类型
cout << "数组第一个元素: " << my_array.front() << endl;
cout << "数组最后一个元素: " << my_array.back() << endl;
array<char,5> char_array = {'h','e','l','l','o'};
cout << char_array.data() << endl;//.data()返回array第一个元素的地址
return 0;
}
结果:
1
10
数组第一个元素: 1
数组最后一个元素: 13
hello
Modifiers
代码:
#include<iostream>
#include<array>
using namespace std;
int main(){
array<int,5> my_array = {1};
array<int,5> your_array = {1,2,3,4,5};
my_array.fill(2);//将数组元素全部赋为一个值,如果之前有值,将会被覆盖掉
for(auto i : my_array){
cout << i << endl;
}
my_array.swap(your_array);//交换两个格式一样array所有元素的值
for(auto i : your_array){
cout << i << endl;
}
for(auto i : my_array){
cout << i << endl;
}
return 0;
}
结果;
2
2
2
2
2
2
2
2
2
2
1
2
3
4
5
Non-member function overloads
代码:
#include <iostream>
#include <array>
int main ()
{
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 and b are equal\n";
if (b!=c) std::cout << "b and c are not equal\n";
if (b<c) std::cout << "b is less than c\n";
if (c>b) std::cout << "c is greater than b\n";
if (a<=b) std::cout << "a is less than or equal to b\n";
if (a>=b) std::cout << "a is greater than or equal to b\n";
return 0;
}
结果:
a and b are equal
b and c are not equal
b is less than c
c is greater than b
a is less than or equal to b
a is greater than or equal to b
比较两个array之间的大小关系,“==”、“<=”、“>=”、“!=”是对每一个元素进行比较,如果所有元素都符合才为真。“<”、“>”则比较第一个不相等的元素。