- C 标准库
- C 标准库
- C++ 标准库
- C++ 库 - 首页
- C++ 库 - <fstream>
- C++ 库 - <iomanip>
- C++ 库 - <ios>
- C++ 库 - <iosfwd>
- C++ 库 - <iostream>
- C++ 库 - <istream>
- C++ 库 - <ostream>
- C++ 库 - <sstream>
- C++ 库 - <streambuf>
- C++ 库 - <atomic>
- C++ 库 - <complex>
- C++ 库 - <exception>
- C++ 库 - <functional>
- C++ 库 - <limits>
- C++ 库 - <locale>
- C++ 库 - <memory>
- C++ 库 - <new>
- C++ 库 - <numeric>
- C++ 库 - <regex>
- C++ 库 - <stdexcept>
- C++ 库 - <string>
- C++ 库 - <thread>
- C++ 库 - <tuple>
- C++ 库 - <typeinfo>
- C++ 库 - <utility>
- C++ 库 - <valarray>
- C++ STL 库
- C++ 库 - <array>
- C++ 库 - <bitset>
- C++ 库 - <deque>
- C++ 库 - <forward_list>
- C++ 库 - <list>
- C++ 库 - <map>
- C++ 库 - <multimap>
- C++ 库 - <queue>
- C++ 库 - <priority_queue>
- C++ 库 - <set>
- C++ 库 - <stack>
- C++ 库 - <unordered_map>
- C++ 库 - <unordered_set>
- C++ 库 - <vector>
- C++ 库 - <algorithm>
- C++ 库 - <iterator>
- C++ 高级库
- C++ 库 - <any>
- C++ 库 - <barrier>
- C++ 库 - <bit>
- C++ 库 - <chrono>
- C++ 库 - <cinttypes>
- C++ 库 - <clocale>
- C++ 库 - <condition_variable>
- C++ 库 - <coroutine>
- C++ 库 - <cstdlib>
- C++ 库 - <cstring>
- C++ 库 - <cuchar>
- C++ 库 - <charconv>
- C++ 库 - <cfenv>
- C++ 库 - <cmath>
- C++ 库 - <ccomplex>
- C++ 库 - <expected>
- C++ 库 - <format>
- C++ 库 - <future>
- C++ 库 - <flat_set>
- C++ 库 - <flat_map>
- C++ 库 - <filesystem>
- C++ 库 - <generator>
- C++ 库 - <initializer_list>
- C++ 库 - <latch>
- C++ 库 - <memory_resource>
- C++ 库 - <mutex>
- C++ 库 - <mdspan>
- C++ 库 - <optional>
- C++ 库 - <print>
- C++ 库 - <ratio>
- C++ 库 - <scoped_allocator>
- C++ 库 - <semaphore>
- C++ 库 - <source_location>
- C++ 库 - <span>
- C++ 库 - <spanstream>
- C++ 库 - <stacktrace>
- C++ 库 - <stop_token>
- C++ 库 - <syncstream>
- C++ 库 - <system_error>
- C++ 库 - <string_view>
- C++ 库 - <stdatomic>
- C++ 库 - <variant>
- C++ STL 库速查表
- C++ STL - 速查表
C++ unordered_set::reserve() 函数
C++ 的unordered_set::reserve()函数用于更改桶的容量。它将容器中的桶数 (bucket_count) 设置为最适合容纳至少 n 个元素且不超过最大负载因子的值,并重新哈希容器。
当 n 大于当前 bucket_count 乘以 max_load_factor 时,会强制进行重新哈希,并且容器桶计数的增加会导致重新哈希。
语法
以下是 std::unordered_set::reserve() 函数的语法。
void reserve ( size_type n );
参数
- n − 表示桶的最小数量。
返回值
此函数不返回任何值。
示例 1
让我们来看下面的例子,我们将演示 unordered_set::reserve() 函数的用法。
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet; uSet.reserve(3); uSet.insert("android"); uSet.insert("java"); uSet.insert("html"); cout << "uSet contains:"; for (const string& x: uSet) cout << " " << x; cout << endl; cout<<"After reserve bucket count is: "<< uSet.bucket_count(); return 0; }
输出
如果我们运行上面的代码,它将生成以下输出:
uSet contains: html java android After reserve bucket count is: 3
示例 2
在下面的例子中,我们将计算使用 unordered_set::reserve() 函数前后桶的数量。
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet = {"android", "java", "Html", "CSS"}; cout<<"Initial bucket count is: "<< uSet.bucket_count() << endl; uSet.reserve(3); cout << "uSet contains:"; for (const string& x: uSet) cout << " " << x; cout << endl; cout<<"After reserve bucket count is: "<< uSet.bucket_count(); return 0; }
输出
以下是上述代码的输出:
Initial bucket count is: 13 uSet contains: android java Html CSS After reserve bucket count is: 5
示例 3
考虑下面的例子,我们将显示使用 unordered_set::reserve() 函数前后桶及其元素。
#include <iostream> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet={"Hyderabad", "Delhi", "Bangalore"}; cout<<"unordered_set contains "<<uSet.bucket_count()<<" buckets:"; for(unsigned int i = 0; i < uSet.bucket_count(); i++) { cout<<"\nThe bucket "<<i<<" contains: "; for(auto it = uSet.begin(i); it != uSet.end(i); ++it) { cout<<*it<<" "; } } cout<<"\n***Capacity is changed using reserve function***\n"; uSet.reserve(5); cout<<"unordered_set contains "<<uSet.bucket_count()<<" buckets:"; for(unsigned int i = 0; i < uSet.bucket_count(); i++) { cout<<"\nThe bucket "<<i<<" contains: "; for(auto it = uSet.begin(i); it != uSet.end(i); ++it) { cout<<*it<<" "; } } return 0; }
输出
上述代码的输出如下:
unordered_set contains 13 buckets: The bucket 0 contains: Bangalore The bucket 1 contains: The bucket 2 contains: Hyderabad The bucket 3 contains: Delhi The bucket 4 contains: The bucket 5 contains: The bucket 6 contains: The bucket 7 contains: The bucket 8 contains: The bucket 9 contains: The bucket 10 contains: The bucket 11 contains: The bucket 12 contains: ***Capacity is changed using reserve function*** unordered_set contains 5 buckets: The bucket 0 contains: The bucket 1 contains: Bangalore The bucket 2 contains: The bucket 3 contains: Delhi The bucket 4 contains: Hyderabad
广告