array是C++11中新提出来的容器类型,与内置数组相比,array是一种更容易使用,更加安全的数组类型,可以用来替代内置数组。作为数组的升级版,继承了数组最基本的特性,也融入了很多容器操作。array和数组一样,是一种固定大小的容器类型,在定义的时候就要声明大小和类型。
1、Iterators
begin():返回第一个元素的迭代器
end():返回最末元素的迭代器
rbegin():返回尾部的逆迭代器
rend():返回起始的逆迭代器
cbegin():返回 const 的begin()
cend():返回 const 的end()
crbegin():返回 const 的 rbegin()
crend():返回 const 的 rend()
示例
void IteratorsTest()
{
std::array<int,5> myarray = { 2, 16, 77, 34, 50 };
std::cout << "myarray contains:";
for ( auto it = myarray.begin(); it != myarray.end(); ++it )
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "myarray const contains:";
for ( auto it = myarray.cbegin(); it != myarray.cend(); ++it )
std::cout << ' ' << *it; // cannot modify *it
std::cout << '\n';
std::cout << "myarray resever contains:";
for ( auto rit=myarray.rbegin() ; rit < myarray.rend(); ++rit )
std::cout << ' ' << *rit;
std::cout << '\n';
std::cout << "myarray resever const backwards:";
for ( auto rit=myarray.crbegin() ; rit < myarray.crend(); ++rit )
std::cout << ' ' << *rit; // cannot modify *rit
std::cout << '\n';
}
运行结果:
myarray contains: 2 16 77 34 50
myarray const contains: 2 16 77 34 50
myarray resever contains: 50 34 77 16 2
myarray resever const backwards: 50 34 77 16 2
2、Capacity
size(): 返回元素个数
max_size(): 返回vector最大能放元素个数
empty():容器是否为空
示例
void CapacityTest()
{
std::array<int,5> myints;
std::cout << "size of myints: " << myints.size() << std::endl;
std::cout << "sizeof(myints): " << sizeof(myints) << std::endl;
std::cout << "max_size of myints: " << myints.max_size() << '\n';
std::array<int,0> first;
std::array<int,5> second;
std::cout << "first " << (first.empty() ? "is empty" : "is not empty") << '\n';
std::cout << "second " << (second.empty() ? "is empty" : "is not empty") << '\n';
}
运行结果
size of myints: 5
sizeof(myints): 20
max_size of myints: 5
first is empty
second is not empty
3、Element access
operator[]: 同数组方式取值
at():返回第n个元素的引用
front():返回第一个元素的引用
back():返回最后一个元素的引用
data():返回第一个元素的指针
示例:
void ElementAccess()
{
std::array<int,10> myarray;
for (int i=0; i<10; i++)
myarray[i]=i;
std::cout << "myarray contains:";
for (int i=0; i<10; i++)
std::cout << ' ' << myarray.at(i);
std::cout << '\n';
std::cout << "front is: " << myarray.front() << std::endl;
std::cout << "back is: " << myarray.back() << std::endl;
const char* cstr = "Test string";
std::array<char,12> charray;
std::memcpy (charray.data(),cstr,12);
std::cout << charray.data() << '\n';
}
运行结果:
myarray contains: 0 1 2 3 4 5 6 7 8 9
front is: 0
back is: 9
Test string
4、Modifiers
fill():设置所有元素的值
swap():交换容器内容
示例:
void ModifiersTest()
{
std::array<int,6> myarray;
myarray.fill(5);
std::cout << "myarray contains:";
for ( int& x : myarray)
{ std::cout << ' ' << x; }
std::cout << '\n';
std::array<int,5> first = {10, 20, 30, 40, 50};
std::array<int,5> second = {11, 22, 33, 44, 55};
first.swap (second);
std::cout << "first:";
for (int& x : first) std::cout << ' ' << x;
std::cout << '\n';
std::cout << "second:";
for (int& x : second) std::cout << ' ' << x;
std::cout << '\n';
}
运行结果:
myarray contains: 5 5 5 5 5 5
first: 11 22 33 44 55
second: 10 20 30 40 50