unordered_multimap 在 C++ STL 中的 rehash() 函式


C++ STL 中的 unordered_multimap rehash(N) 函式將容器中的 bucket 數設定為 n 或更多。如果 n 大於容器中 bucket 的當前數量,則強制進行重新雜湊。新的 bucket 數可以等於或大於 n。如果 n 低於容器中 bucket 的當前數量,函式可能對 bucket 數沒有影響,也可能不會強制重新雜湊。Rehash () 不返回任何內容,並以 n 為引數,該引數指定容器雜湊表中 bucket 的最小數量。

演算法

Begin
   Declaring an empty map container m.
   Force the reahash() function to restrict number of bucket in a
   container to a minimum amount.
   Insert the key value pairs in the container atleast same as the
   minimum number of buckets.
   Print the elements in the map container. 
End.

示例程式碼

 線上演示

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

int main() {
   unordered_map<char, int> m;

   m.rehash(1);
   m.insert (pair<char, int>('b', 10));
   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 << "} ";
   }
   return 0;
}

輸出

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

更新於: 30-Jul-2019

52 人看過

開啟您的 職業生涯

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.