该数组是用于固定大小数组的容器。此容器环绕固定大小的数组,并且在衰减为指针时也不会丢失其长度信息。
为了利用数组,我们需要包括数组头:
#include
让我们来看一个例子。
// CPP program to demonstrate working of array
#include
#include
#include
#include
#include
using namespace std;
int main() {
// construction uses aggregate initialization
// double-braces required
array ar1{{3, 4, 5, 1, 2}};
array ar2 = {1, 2, 3, 4, 5};
array ar3 = {{string("a"), "b"}};
cout << "Sizes of arrays are" << endl;
cout << ar1.size() << endl;
cout << ar2.size() << endl;
cout << ar3.size() << endl;
cout << "\nInitial ar1 : ";
for (auto i : ar1)
cout << i << ' ';
// container operations are supported
sort(ar1.begin(), ar1.end());
cout << "\nsorted ar1 : ";
for (auto i : ar1)
cout << i << ' ';
// Filling ar2 with 10
ar2.fill(10);
cout << "\nFilled ar2 : ";
for (auto i : ar2)
cout << i << ' ';
// ranged for loop is supported
cout << "\nar3 : ";
for (auto &s : ar3)
cout << s << ' ';
return 0;
}
输出:
Sizes of arrays are
5
5
2
Initial ar1 : 3 4 5 1 2
sorted ar1 : 1 2 3 4 5
Filled ar2 : 10 10 10 10 10
ar3 : a b
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。