C++ 向量::max_size() 函數
vector::max_size() 是 "vector" 頭文件的庫函數,用於獲取向量的最大大小,返回向量可以存儲的元素總數。
注意:要使用矢量,請包括<vector>
標題。
vector::max_size() 函數的語法
vector::max_size();
參數: none
——它什麽都不接受。
返回值: size_type
– 它以無符號整數類型返回向量的最大大小。
例:
Input: vector<int> vector1{ 1, 2, 3, 4, 5 }; Function call: cout << vector1.max_size(); Output: 4611686018427387903
演示vector::max_size()函數示例的C++程序
//C++ STL program to demonstrate example of
//vector::max_size() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
//printing the max_size of the vector
cout << "Maximum number of elements that can be stored:";
cout << v1.max_size() << endl;
//pushing elements
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//printing the max_size of the vector
cout << "Maximum number of elements that can be stored:";
cout << v1.max_size() << endl;
return 0;
}
輸出
Maximum number of elements that can be stored:4611686018427387903 Maximum number of elements that can be stored:4611686018427387903
參考:C++ 向量::max_size()
相關用法
- C++ vector::rbegin()用法及代碼示例
- C++ vector::pop_back()用法及代碼示例
- C++ vector::crend()用法及代碼示例
- C++ vector::push_back()用法及代碼示例
- C++ vector::emplace_back用法及代碼示例
- C++ vector::at()用法及代碼示例
- C++ vector::swap()用法及代碼示例
- C++ vector::shrink_to_fit()用法及代碼示例
- C++ vector::cbegin()用法及代碼示例
- C++ vector::back()用法及代碼示例
- C++ vector::assign()用法及代碼示例
- C++ vector::begin()用法及代碼示例
- C++ vector::cend()用法及代碼示例
- C++ vector::operator[]用法及代碼示例
- C++ vector::clear()用法及代碼示例
- C++ vector::cbegin()、vector::cend()用法及代碼示例
- C++ vector::empty()用法及代碼示例
- C++ vector::front()、vector::back()用法及代碼示例
- C++ vector::at()、vector::swap()用法及代碼示例
- C++ vector::crbegin()用法及代碼示例
注:本文由純淨天空篩選整理自 vector::max_size() function with example in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。