array是固定大小的序列容器。属于std命名空间。
1、容器相关:注意××end()迭代器指向的位置是无效的。
非const修饰的迭代器:
begin():返回指向容器内部第一个元素的迭代器
iterator begin() noexcept; const_iterator begin() const noexcept;
end():返回指向容器内部最后一个元素的下一个位置迭代器
iterator end() noexcept; const_iterator end() const noexcept;
rbegin():返回一个反向迭代器,指向容器的最后一个元素
reverse_iterator rbegin() noexcept; const_reverse_iterator rbegin() const noexcept;
rend():返回一个反向迭代器,指向容器第一个元素前面的位置
reverse_iterator rend() noexcept; const_reverse_iterator rend() const noexcept;
const修饰的迭代器。功能与非const迭代器一样。
cbegin():返回的const迭代器
const_iterator cbegin() const noexcept;
cend():返回的const迭代器
const_iterator cend() const noexcept;
crbegin():返回const反向迭代器
const_reverse_iterator crbegin() const noexcept;
crend()返回const反向迭代器
const_reverse_iterator crend() const noexcept;
size():返回容器中元素的个数
constexpr size_type size() noexcept;
max_size():返回容器所能容纳的元素的最大个数
constexpr size_type max_size() noexcept;
empty():测试数组array是否为空,返回一个bool值标识array是否为空。
constexpr bool empty() noexcept;
3、元素访问
operator[]:返回下标为n的元素的引用
reference operator[] (size_type n); const_reference operator[] (size_type n) const;
at:返回下标为n的元素的引用
reference at ( size_type n ); const_reference at ( size_type n ) const;
front:返回容器中第一个元素的引用
reference front(); const_reference front() const;
back:返回容器中最后一个元素的引用
reference back(); const_reference back() const;
data:返回指向第一个元素的指针
value_type* data() noexcept; const value_type* data() const noexcept;
4、关于修改的函数
fill():填充数组,将array中的元素均填充为val。
void fill (const value_type& val);
swap():交换内容,将array x的元素值与调用swap的array对象的值调换。
void swap (array& x) noexcept(noexcept(swap(declval<value_type&>(),declval<value_type&>())));
例子:
#include <iostream> #include <array> using namespace std; array <int,5>globalarray;//默认初始化为0 void arrdefine_test() { array<int,5>first; //未初始化,元素值不确定 array<int,5>second = {1,2,3,4,5}; //初始化 array<int,5>third = second; //复制初始化 cout<<"global array"<<endl; for(int i = 0; i < 5; i++) cout << globalarray[i] << " "; cout << endl; cout<<"first"<<endl; for(int i = 0; i < 5; i++) cout << first[i] << " "; cout << endl; cout << "second"<< endl; for(int i = 0; i < 5; i++) cout << second[i] << " "; cout << endl<<"third"<<endl; for(int i = 0; i < 5; i++) cout << third[i] << " "; cout << endl; } void iterator_test() { array<int, 5> myarr = {3,4,5,6,7}; array<int, 5>::iterator arriter; array<int, 5>::reverse_iterator rarriter; array<int, 5>::const_iterator carriter; array<int, 5>::const_reverse_iterator crarriter; /* auto it; for(it = myarr.begin(); it != myarr.end(); it++){ cout << *it ; } cout << endl; */ //begin()\end()返回iterator和const_iterator两种迭代器 for(arriter = myarr.begin(); arriter != myarr.end(); arriter++){ cout << *arriter ; }//34567 cout << endl; for(carriter = myarr.begin(); carriter != myarr.end(); carriter++){ cout << *carriter ; }//34567 cout << endl; //cbegin()\cend()值返回const_iterator一种迭代器 for(carriter = myarr.cbegin(); carriter != myarr.cend(); carriter++){ cout << *carriter ; }//34567 cout << endl; //rbegin()\rend()返回reverse_iterator和const_reverse_iterator两种类型的迭代器 for(rarriter = myarr.rbegin(); rarriter != myarr.rend(); rarriter++){ cout << *rarriter; }//76543 cout << endl; for(crarriter = myarr.rbegin(); crarriter != myarr.rend(); crarriter++){ cout << *crarriter; }//76543 cout << endl; //crbegin()和crend()只返回const_reverse_iterator一种迭代器 for(crarriter = myarr.crbegin(); crarriter != myarr.crend(); crarriter++){ cout << *crarriter ; }//76543 cout << endl; } void access_test() { array<int,20> myarr; int mfront; int mback; int *data; int mdata; for(int i = 0; i < 10; i++) myarr[i] = i; for(int i = 0; i < 10; i++) cout << myarr[i] << " "; cout << endl; for(int i = 10; i < 20; i++) myarr.at(i) = i; for(int i = 10; i < 20; i++) cout << myarr.at(i) << " "; cout << endl; mdata = myarr.at(16); cout << "myarr.at(16): "<< mdata << endl; mdata = myarr[13]; cout << "myarr[13]"<< mdata << endl; mfront = myarr.front(); cout << "front: " << mfront << endl; mback = myarr.back(); cout <<" back: " << mback << endl; data = myarr.data(); cout <<"data: "<< *data << endl; } void capacity_test() { array<int,10>myarr; array<int,0>myarr2; for(int i = 0; i < 5; i++){ myarr[i] = i; } cout <<"size:"<< myarr.size() << endl;//10 cout <<"sizeof(myarr):"<<sizeof(myarr)<<endl; //字节数40 cout << "max_size:"<<myarr.max_size() << endl; //10 cout << "empty:"<< (myarr.empty()?"true":"false") << endl; //false cout << "empty:"<<(myarr2.empty()?"true":"false") << endl;//true } void modefy_test() { array<int,10> myarr; //array<int, 10>其实是一种类似于int的数据类型,可以用于定义变量myarr。 array<int,10> youarr;//array<int, 5>或array<int, 10>均是不同的类型。 myarr.fill(17); for(int &x:myarr)cout <<" "<<x; cout << endl; youarr.fill(100); for(int i = 0; i < 10; i++) cout << " " << youarr[i]; cout << endl; cout<<"myarr.swap(youarr)"<<endl; //swap()函数的两个对象必须元素个数一致,元素的类型也要一致。 myarr.swap(youarr); //使用成员函数swap() for(int &x:myarr)cout << " " << x; cout << endl; for(int&x:youarr)cout << " " << x; cout << endl; cout<<"swap(myarr,youarr)"<<endl; swap(myarr,youarr); //使用的算法函数swap() for(int &x:myarr)cout << " " << x; cout << endl; for(int&x:youarr)cout << " " << x; cout << endl; } int main() { arrdefine_test(); iterator_test(); access_test(); capacity_test(); modefy_test(); return 0; }结果是:
global array
0 0 0 0 0
first
-1219775224 -1080231112 134518057 1 65535
second
1 2 3 4 5
third
1 2 3 4 5
34567
34567
34567
76543
76543
76543
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
myarr.at(16): 16
myarr[13]13
front: 0
back: 19
data: 0
size:10
sizeof(myarr):40
max_size:10
empty:false
empty:true
17 17 17 17 17 17 17 17 17 17
100 100 100 100 100 100 100 100 100 100
myarr.swap(youarr)
100 100 100 100 100 100 100 100 100 100
17 17 17 17 17 17 17 17 17 17
swap(myarr,youarr)
17 17 17 17 17 17 17 17 17 17
100 100 100 100 100 100 100 100 100 100