unordered_multiset :: rehash()是C++ STL中的内置函数,它将容器中的存储桶数设置为N或更大。如果N大于容器中的当前桶数(bucket_count),则将强制进行重新哈希处理。
新的存储桶计数可以等于或大于N。如果n小于容器中当前存储桶的数量(bucket_count),则该函数可能对存储桶计数没有影响,并且可能不会强制进行重新哈希。
重新哈希是哈希表的重建:容器中的所有元素根据其哈希值重新排列到新的存储桶集中。尽管可以保留具有等效键的元素的相对顺序,但是这可能会更改容器内元素的迭代顺序。只要容器的负载因子在操作中超过其max_load_factor,容器就会自动执行重整。通过调用rehash在哈希表中保留一定数量的最小存储桶,我们避免了容器扩展可能导致的多次rehash。
句法:
unordered_multset_name.rehash(N);
参数:该函数仅接受一个参数,如下所示:
- count :这是新的存储桶数。
返回值:无(void类型函数)
下面的程序说明了上述方法:
程序1:
// C++ program to illustrate
// unordered_multiset::rehash()
#include
#include
using namespace std;
// function to diplay values in multiset
void display(unordered_multiset s)
{
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << endl;
}
int main()
{
// declaration
unordered_multiset s1;
// inserting initial values
s1.insert(1);
s1.insert(2);
s1.insert(3);
// displaying Initial values
cout << "Initial values are:\n";
display(s1);
cout << endl;
// diplaying initial parameters
cout << "initial parameters are: \n";
cout << "bucketcount() = " << s1.bucket_count() << endl;
cout << "load factor = " << s1.load_factor() << endl;
cout << "Max_load_factor = " << s1.max_load_factor() << endl;
cout << endl;
// performing rehash
s1.rehash(200);
// displaying final parameters
cout << "final parameters are: \n";
cout << "bucketcount() = " << s1.bucket_count() << endl;
cout << "load factor = " << s1.load_factor() << endl;
cout << "Max_load_factor = " << s1.max_load_factor() << endl;
cout << endl;
return 0;
}
输出:
Initial values are:
3
1
2
initial parameters are:
bucketcount() = 7
load factor = 0.428571
Max_load_factor = 1
final parameters are:
bucketcount() = 211
load factor = 0.014218
Max_load_factor = 1
程式2:
// C++ program to illustrate
// unordered_multiset::rehash()
#include
#include
using namespace std;
// function to diplay values in multiset
void display(unordered_multiset s)
{
for (auto it = s.begin(); it != s.end(); it++)
cout << *it << endl;
}
int main()
{
// declaration
unordered_multiset s1;
// inserting initial values
s1.insert('a');
s1.insert('b');
s1.insert('c');
// displaying Initial values
cout << "Initial values are:\n";
display(s1);
cout << endl;
// diplaying initial parameters
cout << "initial parameters are: \n";
cout << "bucketcount() = " << s1.bucket_count() << endl;
cout << "load factor = " << s1.load_factor() << endl;
cout << "Max_load_factor = " << s1.max_load_factor() << endl;
cout << endl;
// performing rehash
s1.rehash(200);
// displaying final parameters
cout << "final parameters are: \n";
cout << "bucketcount() = " << s1.bucket_count() << endl;
cout << "load factor = " << s1.load_factor() << endl;
cout << "Max_load_factor = " << s1.max_load_factor() << endl;
cout << endl;
return 0;
}
输出:
Initial values are:
c
a
b
initial parameters are:
bucketcount() = 7
load factor = 0.428571
Max_load_factor = 1
final parameters are:
bucketcount() = 211
load factor = 0.014218
Max_load_factor = 1
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。