ฟังก์ชัน unordered_multimap rehash(N) ใน C++ STL กำหนดจำนวนที่เก็บข้อมูลในคอนเทนเนอร์เป็น 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}