更改 max_load_factor() 导致 std::unordered_map 中出现段错误
Changing max_load_factor() causing segfault in std::unordered_map
我有 std::unordered_map
,我已经用存储桶大小 100 初始化了它。当我更改 max_load_factor
时,代码在访问 bucket_size()
时出现段错误。我在 linux 上使用 g++ 作为编译器。我想增加负载因子以便元素发生碰撞。
1> 为什么我会出现段错误?
2> 为 unordered_map
设置 max_load_factor
的正确方法是什么?据我所知 std::unordered_map
的构造函数不接受负载因子作为参数。
没有设置的代码max_load_factor,没有问题
// unordered_map::bucket_size
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main ()
{
std::unordered_map<int, std::string> mymap(10);
unsigned nbuckets = mymap.bucket_count();
std::cout << "mymap has " << nbuckets << " buckets:\n";
std::cout << "mymap load factor " << mymap.max_load_factor() << endl;
for (unsigned i=0; i<nbuckets; ++i) {
std::cout << "bucket #" << i << " has " << mymap.bucket_size(i) << " elements.\n";
}
return 0;
}
输出
$ g++ -std=c++11 map.cpp && ./a.out
mymap has 11 buckets:
mymap load factor 1
bucket #0 has 0 elements.
bucket #1 has 0 elements.
bucket #2 has 0 elements.
bucket #3 has 0 elements.
bucket #4 has 0 elements.
bucket #5 has 0 elements.
bucket #6 has 0 elements.
bucket #7 has 0 elements.
bucket #8 has 0 elements.
bucket #9 has 0 elements.
bucket #10 has 0 elements.
现在,一旦我引入更改 max_load_factor 的代码,我就会遇到段错误。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main ()
{
std::unordered_map<int, std::string> mymap(10);
unsigned nbuckets = mymap.bucket_count();
std::cout << "mymap has " << nbuckets << " buckets:\n";
mymap.max_load_factor(10);
std::cout << "mymap load factor " << mymap.max_load_factor() << endl;
for (unsigned i=0; i<nbuckets; ++i) {
std::cout << "bucket #" << i << " has " << mymap.bucket_size(i) << " elements.\n";
}
return 0;
}
输出
$ g++ -std=c++11 map.cpp && ./a.out
mymap has 11 buckets:
mymap load factor 10
bucket #0 has 0 elements.
bucket #1 has 0 elements.
bucket #2 has 0 elements.
Segmentation fault
我怀疑无法保证容器在 max_load_factor
发生变化后是否会保持相同的桶数。如果是这种情况,您可能会迭代无效数量的桶,因为 nbuckets
是在代码的前面定义的。
我有 std::unordered_map
,我已经用存储桶大小 100 初始化了它。当我更改 max_load_factor
时,代码在访问 bucket_size()
时出现段错误。我在 linux 上使用 g++ 作为编译器。我想增加负载因子以便元素发生碰撞。
1> 为什么我会出现段错误?
2> 为 unordered_map
设置 max_load_factor
的正确方法是什么?据我所知 std::unordered_map
的构造函数不接受负载因子作为参数。
没有设置的代码max_load_factor,没有问题
// unordered_map::bucket_size
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main ()
{
std::unordered_map<int, std::string> mymap(10);
unsigned nbuckets = mymap.bucket_count();
std::cout << "mymap has " << nbuckets << " buckets:\n";
std::cout << "mymap load factor " << mymap.max_load_factor() << endl;
for (unsigned i=0; i<nbuckets; ++i) {
std::cout << "bucket #" << i << " has " << mymap.bucket_size(i) << " elements.\n";
}
return 0;
}
输出
$ g++ -std=c++11 map.cpp && ./a.out
mymap has 11 buckets:
mymap load factor 1
bucket #0 has 0 elements.
bucket #1 has 0 elements.
bucket #2 has 0 elements.
bucket #3 has 0 elements.
bucket #4 has 0 elements.
bucket #5 has 0 elements.
bucket #6 has 0 elements.
bucket #7 has 0 elements.
bucket #8 has 0 elements.
bucket #9 has 0 elements.
bucket #10 has 0 elements.
现在,一旦我引入更改 max_load_factor 的代码,我就会遇到段错误。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main ()
{
std::unordered_map<int, std::string> mymap(10);
unsigned nbuckets = mymap.bucket_count();
std::cout << "mymap has " << nbuckets << " buckets:\n";
mymap.max_load_factor(10);
std::cout << "mymap load factor " << mymap.max_load_factor() << endl;
for (unsigned i=0; i<nbuckets; ++i) {
std::cout << "bucket #" << i << " has " << mymap.bucket_size(i) << " elements.\n";
}
return 0;
}
输出
$ g++ -std=c++11 map.cpp && ./a.out
mymap has 11 buckets:
mymap load factor 10
bucket #0 has 0 elements.
bucket #1 has 0 elements.
bucket #2 has 0 elements.
Segmentation fault
我怀疑无法保证容器在 max_load_factor
发生变化后是否会保持相同的桶数。如果是这种情况,您可能会迭代无效数量的桶,因为 nbuckets
是在代码的前面定义的。