Computer >> Máy Tính >  >> Lập trình >> C ++

Hàm unsrdered_multimap Reserve () trong C ++ STL

Hàm unardered_multimap Reserve () trong C ++ STL đặt số lượng nhóm trong vùng chứa thành số thích hợp nhất để nó chứa ít nhất n phần tử.

Nếu n lớn hơn số lượng nhóm hiện tại nhân với max_load_factor, thì số lượng nhóm của vùng chứa sẽ tăng lên và buộc phải thực hiện lại.

Reserve () không trả về gì và lấy n làm tham số chỉ định số lượng phần tử tối thiểu theo dung lượng tối thiểu được yêu cầu.

Thuật toán

Begin
   Declare the vector m.
   m.reserve(6) = the size is reserved for the bucket to contain minimum number of one elements.
   Insert the key value pairs.
   Print the result.
End.

Mã mẫu

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

int main() {
   unordered_map<char, int> m;      //declaring m as map container

   m.reserve(6);//restricting the most appropriate value of
   m.insert (pair<char, int>('b', 10)); // inserting values
   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 << "} "; //printing the values of map container
   }
   return 0;
}

Đầu ra

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