📜  C++ STL中的array :: max_size()

📅  最后修改于: 2021-05-30 13:24:57             🧑  作者: Mango

数组类通常比C型数组更有效,更轻量且更可靠。 C++ 11中数组类的引入为C样式数组提供了更好的替代方法。

array :: max_size()

此函数返回数组容器可以包含的最大元素数。
如果是数组,则size()和max_size()函数始终返回相同的值

句法 :

arrayname.max_size()
Parameters :
No parameter is passed.
Returns :
It returns the maximum number of
elements that the array can contain.

例子:

Input  : myarray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
         myarray.max_size();
Output : 10

Input  : myarray = {1, 2, 3, 4, 5};
         myarray.max_size();
Output : 5

错误和异常

1. It throws an error if a parameter is passed.
2. It has a no exception throw guarantee otherwise.
// CPP program to illustrate
// Implementation of max_size() function
#include 
#include 
using namespace std;
  
int main()
{
    array myarray{ 1, 2, 3, 4, 5 };
    cout << myarray.max_size();
    return 0;
}

输出:

5
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”