C++ STL 中的 map max_size()
在本文中,我們將討論 C++ STL 中 map::max_size() 函式的工作原理、語法和示例。
什麼是 C++ STL 中的對映?
對映是關聯容器,它有助於按特定順序儲存由鍵值和對映值組合而成的元素。在對映容器中,資料始終藉助其關聯鍵內部排序。透過其唯一鍵訪問對映容器中的值。
什麼是 map::max_size()?
map::max_size() 函式是 C++ STL 中的一個內建函式,在 <<map> 標頭檔案中定義。max_size() 用於返回對映容器的最大大小。
此函式用於檢查對映容器可以容納的最大值。該大小類似於容器的潛力,因此不能保證是否可以達到該值。
語法
Map_name.max_size();
引數
該函式不接受任何引數。
返回值
此函式返回容器可以容納的元素數量。
輸入
map<char, int> newmap; newmap.max_size();
輸出
Max size of map is: 461168601842738790
示例
#include <bits/stdc++.h> using namespace std; int main(){ map<int, int> TP_1, TP_2; TP_1.insert({ 0, 10 }); cout<<"Max size of map with elements is: " << TP_1.max_size(); cout<<"\Max size of map without elements is: " << TP_2.max_size(); return 0; }
輸出
如果我們執行以上程式碼,它將生成以下輸出 −
Max size of map with elements is: 461168601842738790 Max size of map without elements is: 461168601842738790
廣告