Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++ STL의 unordered_multimap reserved() 함수

<시간/>

C++ STL의 unordered_multimap reserved() 함수는 컨테이너의 버킷 수를 가장 적절한 수로 설정하여 n개 이상의 요소를 포함하도록 합니다.

n이 현재 버킷 수에 max_load_factor를 곱한 값보다 크면 컨테이너의 버킷 수가 증가하고 강제로 rehash됩니다.

Reserve()는 아무 것도 반환하지 않고 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.

예시 코드

#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;
}

출력

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