stl标准模板库_C ++标准模板库(STL)中的数组及其常用功能
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 initializationarray<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;elsecout<<"Array is not empty!!!"<<endl;//Array functionscout<<"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 50Reference: C++ std::array
參考: C ++ std :: array
翻譯自: https://www.includehelp.com/stl/array-in-cpp-standard-template-library-with-its-common-functions.aspx
stl標準模板庫
總結
以上是生活随笔為你收集整理的stl标准模板库_C ++标准模板库(STL)中的数组及其常用功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 京东二面:MySQL 主从延迟、读写分离
- 下一篇: ThreadLocal内存溢出代码演示和