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}
广告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP