Arrays are fixed-size sequence containers.
c++11 中的array是固定大小的的线性序列容器
特点:
1. 他是严格的序列化排序的线性容器。
2. 连续存储的。
3. 大小固定,不能动态拓展。
array 使用构造函数和析构函数管理空间, 容器大小是编译时常量。没有空间和时间开销。。所以没有。push_back, pop_back等成员函数。
初始化方法
array<int, 5> myArray;
myArray.fill(0);
array<int, 5> myArray2 = {1,2,3,4,5};
array<int, 5> myArray3;
for (int i = 0; i < 5; i++)
{
myArray3[i] = i;
}
std::array::swap
交换两个array条件是必须有相同的类型和容器大小
array的关系运算符操作
#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;
}
Output:
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 |