作者:手机用户2502875023 | 来源:互联网 | 2023-10-11 13:18
array是固定大小的数组,和我们用的 [ ] 形式数组差不多。
特点:
1.  只保存里面元素,连元素大小都不保存  Internally, an array does not keep any data other than the elements it contains (not even its size, which is a template parameter, fixed on compile time)
2.  大小为0是合法的,但是不要解引用。Zero-sized arrays are valid, but they should not be dereferenced (members front, back, and data).
常见操作array
arr {1,2,3,4,5,6};
arr.begin()
Return iterator to beginning (public member function )
arr.end()
Return iterator to end (public member function )
arr.rbegin()? ? ?// 翻转begin
Return reverse iterator to reverse beginning (public member function )
arr.rend()? ? ? ?//? 翻转end
Return reverse iterator to reverse end (public member function )
arr.cbegin()? ? ?// const begin的意思
Return const_iterator to beginning (public member function )
arr.cend()? ? ? // const? end的意思
Return const_iterator to end (public member function )
arr.crbegin()? ? //? const? rbegin 的意思
Return const_reverse_iterator to reverse beginning (public member function )
arr.crend()? ? ?//? const? rbegin 的意思
Return const_reverse_iterator to reverse end (public member function )
?
arr.size()
Return size (public member function )
arr.max_size() // 永远和arr.size()一样是初始化的那个数字即 array 中的6
Return maximum size (public member function )
arr.empty()
Test whether array is empty (public member function )
arr[i] //不检查下标是否越界
Access element (public member function )
arr.at(i) //会检查下标是否越界
Access element (public member function )
arr.front() // 返回引用,可以当左值,第一个元素
Access first element (public member function )
arr.back() // 返回引用,可以当左值,最后一个元素
Access last element (public member function )
arr.data() //返回指向数组的指针
Get pointer to data (public member function )
Modifiers
arr.fill(num) // 把数组中所有元素都填为num
Fill array with value (public member function )
arr.swap(arr2) // 同另外一个数组交换元素,同类型,同大小
Swap content (public member function )
?
当反向遍历时,迭代器++ 其实是迭代器 --
?
  
#include
#include
using namespace std;
int main(void)
{
array test;
cout < cout < cout <<"==============" < array a = {0, 1, 2, 3, 4, 5};
for (auto i : a) {
cout < }
cout <<"==============" < cout < cout <<"==============" < for (auto it = a.data(); it != a.end(); it++) {
cout <<*it < }
cout < //cout < for (auto it = a.rbegin(); it != a.rend(); it++) {
cout <<*it < }
}&#160;