简介
数组是一个固定大小的顺序容器:它以线性序列存储指定数量的元素。
成员函数
迭代器操作
array::begin()
返回一个迭代器指向array的第一个元素。如果array为空,返回值不能被引用。返回类型为array::iterator。
array::end()
返回一个迭代器指向array最后一个元素的下一个位置。返回结果不能被引用。如果array为空,返回值和begin()一致。
array::rbegin()
返回一个反转迭代器指向array的最后一个元素,如果array为空,返回值不能被引用。返回类型为array::reverse_iterator。
array::rend()
返回一个反转迭代器指向array的第一个元素的下一个位置。如果array为空,返回值和rbegin()一致。返回值类型为array::reverse_iterator。
array::cbegin()
begin()的const版本。
array::cend()
end()的const版本。
array::crbegin()
cbegin()的const版本。
array::crend()
rend()的const版本。
示例代码
#include<iostream>
#include<array>
using namespace std;
int
main(void)
{
array<int, 5> arr = {1, 2, 3, 4, 5};
// array::begin end
for(array<int, 5>::iterator it = arr.begin(); it != arr.end(); ++it)
{
cout << *it << "";
}
cout << endl;
// array::rbegin rend
for(array<int, 5>::reverse_iterator it = arr.rbegin(); it != arr.rend(); ++it)
{
cout << *it << "";
}
cout << endl;
for(array<int, 5>::const_iterator it = arr.cbegin(); it != arr.cend(); ++it)
{
cout << *it << "";
}
cout << endl;
for(array<int, 5>::const_reverse_iterator it = arr.crbegin(); it != arr.crend(); ++it)
{
cout << *it << "";
}
cout << endl;
return(0);
}
Capacity
array::size()
返回array中元素个数。返回值类型为array::size_type。
array::max_size()
返回array能存储元素个数的最大值,和size()返回值相等。返回值类型array::size_type。
array::empty()
测试数组是否为空。返回值为bool型,如果size()等于0表示array为空,返回true,否则返回false。
示例程序
#include <iostream>
#include <array>
using namespace std;
int
main(void)
{
array<int, 0> arr1;
array<int, 5> arr2 = {1, 2, 3, 4, 5};
cout << "arr1 size : " << arr1.size() << endl;
cout << "arr2 size : " << arr2.size() << endl;
cout << "arr1 max size : " << arr1.max_size() << endl;
cout << "arr2 max size : " << arr2.max_size() << endl;
cout << "arr1 is " << (arr1.empty() ? "is empty":"is not empty") << endl;
cout << "arr2 is " << (arr2.empty() ? "is empty":"is not empty") << endl;
return 0;
}
Element access
array::operator[]
返回array指定位置的引用。返回类型为reference/const_reference。
array::at
返回array指定位置的引用。返回类型为reference/const_reference。
array::front
返回array第一个元素的引用。返回类型为reference/const_reference。
array::back
返回array最后一个元素的引用。返回类型为reference/const_reference。
array::data
返回指向array第一个元素的指针。
示例程序
#include <iostream>
#include <array>
using namespace std;
int
main(void)
{
array<int, 5> arr = {1, 2, 3, 4, 5};
// array::operator=
cout << "The first element is : " << arr[0] << endl;
arr[0] = 10;
cout << "The first element is : " << arr[0] << endl;
arr[0] = 10;
// array::at
cout << "The second element is : " << arr.at(1) << endl;
arr.at(1) = 20;
cout << "The second element is : " << arr.at(1) << endl;
// array::front
cout << "array::front : " << arr.front() << endl;
// array::back
cout << "array::back : " << arr.back() << endl;
// array::data
int *p;
p = arr.data();
cout << "array::data" << *p << endl;
return(0);
}
Modifiers
array::fill()
向数组填充指定的值。
array::swap()
交换两个数组。两个数组类型和大小必须相同。
示例代码
#include<iostream>
#include<array>
using namespace std;
void
print(array<int, 5> arr)
{
for(int i = 0; i < arr.size(); i++)
{
cout <<arr.at(i) << "";
}
cout << endl;
}
int
main(void)
{
array<int, 5> arr1 = {2, 2, 2, 2, 2};
array<int, 5> arr2 = {1, 2, 3, 4, 5};
// array::fill
cout << "Beforefill() : ";
print(arr1);
arr1.fill(1);
cout << "Afterfill() : ";
print(arr1);
arr1.swap(arr2);
cout << "arr1: ";
print(arr1);
cout << "arr2";
print(arr2);
return(0);
}