unordered_multimap reserve() 函式在 C++ STL 中


C++ STL 中的 unordered_multimap reserve() 函式將容器中的桶數設定為最合適的數量,以使其至少包含 n 個元素。

如果 n 大於當前的桶數乘以 max_load_factor,則會增加容器的桶數並強制進行重新雜湊。

Reserve() 不返回任何內容,並以 n 作為引數,該引數指定按請求的最小容量指定的最小元素數。

演算法

Begin
   Declare the vector m.
   m.reserve(6) = the size is reserved for the bucket to contain minimum number of one elements.
   Insert the key value pairs.
   Print the result.
End.

示例程式碼

 即時演示

#include<iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
   unordered_map<char, int> m;      //declaring m as map container

   m.reserve(6);//restricting the most appropriate value of
   m.insert (pair<char, int>('b', 10)); // inserting values
   m.insert (pair<char, int>('a', 20));

   cout << "The size is: " << m.size();
   cout << "\nKey and values are: ";
   for (auto it = m.begin(); it != m.end(); it++) {
      cout << "{" << it->first << ", " << it->second << "} "; //printing the values of map container
   }
   return 0;
}

輸出

The size is: 2
Key and values are: {a, 20} {b, 10}

更新日期: 30-Jul-2019

109 瀏覽次數

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.