C ++ STL中的unordered_multimap rehash()函数
C++STL中的unordered_multimaprehash(N)函数将容器中的存储桶数设置为n或更大。如果n大于容器中当前的存储桶数,则将强制进行重新哈希处理。新的存储桶计数可以等于或大于n。如果n小于容器中当前的存储桶数,则该功能可能对存储桶数没有影响,也可能不会强制重新哈希。Rehash()不返回任何内容,并以n作为参数,该参数指定容器哈希表的最小存储桶数。
算法
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}