unordered_multimap reserve() 函数在 C++ STL 中


C++ STL 中的 unordered_multimap reserve() 函数将容器中的桶数设置为最合适的数量,以使其至少包含 n 个元素。

如果 n 大于当前的桶数乘以 max_load_factor,则会增加容器的桶数并强制进行重新哈希。

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}

更新日期: 30-Jul-2019

109 浏览次数

开启你的 职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.