stl标准模板库
"array" is a container in C++ STL, which has fixed size, which is defined in "array" header.
“ array”是C ++ STL中的一个容器,具有固定大小,在“ array”标头中定义。
Declaration:
宣言:
array <data_type, size> array_name = {initializer_list};
Example:
array<int,5> values {10, 20, 30, 40, 50};
Array class's common functions:
数组类的常用功能:
array::operator[] - Gets and sets a reference to an element based on given index.
array :: operator [] -根据给定的索引获取并设置对元素的引用。
array.empty() - Returns true if array is empty
array.empty() -如果数组为空,则返回true
array.size() - Returns the total number of elements in the array
array.size() -返回数组中元素的总数
array.front() - Return the first element
array.front() -返回第一个元素
array.back() - Returns the last element
array.back() -返回最后一个元素
array.at(index) - Returns the element from given index
array.at(index) -返回给定索引中的元素
array.begin() - Returns the reference pointing to the first element
array.begin() -返回指向第一个元素的引用
array.end() - Returns the reference punting to the last element
array.end() -返回指向最后一个元素的引用
Example:
例:
#include <iostream>
#include <array>
using namespace std;
int main()
{
//array declaring and initialization
array<int, 5> arr = {10, 20, 30, 40, 50};
//checking array is empty or not by using empty()
if(arr.empty())
cout<<"Array is empty!!!"<<endl;
else
cout<<"Array is not empty!!!"<<endl;
//Array functions
cout<<"size: " << arr.size() <<endl;
cout<<"first element: " << arr.front() <<endl;
cout<<"last element: " << arr.back() <<endl;
cout<<"0th element: " << arr.at(0) <<endl;
cout<<"3rd element: " << arr.at(3) <<endl;
//printing all array elements are: ";
for(auto i = arr.begin () ; i != arr.end(); i++)
cout<<*i<<" ";
cout<<endl;
return 0;
}
Output
输出量
Array is not empty!!!size: 5first element: 10last element: 500th element: 103rd element: 4010 20 30 40 50
Reference: C++ std::array
参考: C ++ std :: array
翻译自: https://www.includehelp.com/stl/array-in-cpp-standard-template-library-with-its-common-functions.aspx
stl标准模板库