目录

unordered_multimap::reserve()

描述 (Description)

C ++函数std::unordered_multimap::reserve()将容器中的桶数设置为最适合包含至少n个元素的桶数。

如果n大于当前的bucket_count()* max_load_factor(),则容器的桶计数增加并且强制重新散列 ,如果n低于该值,则该函数可能无效。

声明 (Declaration)

以下是std :: unordered_multimap :: reserve()函数形式std :: unordered_map()头的声明。

C++11

void reserve(size_type n);

参数 (Parameters)

n - 容器的新容量。

返回值

没有

时间复杂

线性即平均情况下为O(n)。

在最坏的情况下,二次即O(n 2 )。

例子 (Example)

以下示例显示了std :: unordered_multimap :: reserve()函数的用法。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> umm;
   cout << "Initial bucket count = " << umm.bucket_count() << endl;
   umm.reserve(5);
   cout << "Bucket count after reserve = " << umm.bucket_count() << endl;
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Initial bucket count = 11
Bucket count after reserve = 5
↑回到顶部↑
WIKI教程 @2018