Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน unordered_multimap::rehash() ใน C++ STL

ฟังก์ชัน unordered_multimap::rehash(N) ใน C++ STL กำหนดจำนวน Bucket ให้เป็น N หรือมากกว่า บังคับ Rehash ถ้า N มากกว่าจำนวน Bucket ปัจจุบัน จำนวน Bucket ใหม่จะเท่าหรือมากกว่า N อาจไม่มีผลถ้า N น้อยกว่าปัจจุบัน

Rehash() ไม่คืนค่า รับพารามิเตอร์ n ระบุจำนวน Bucket ขั้นต่ำ

อัลกอริทึม

Begin
    Declare empty map container m
    Force rehash() to set minimum buckets
    Insert key-value pairs at least equal to min buckets
    Print elements in map
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}