作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <iostream>
#include <unordered_set>
#include <memory>
enum Config
{
NO_NEW_LINE,
TO_FILE,
NO_CONSOLE
};
int main()
{
std::shared_ptr<std::unordered_set<Config>> configurations;
configurations->emplace(Config::NO_NEW_LINE);
if (configurations->find(Config::NO_NEW_LINE) == configurations->end())
std::cout << "nothing found " << std::endl;
return 0;
}
我不知道为什么这段代码会出现段错误。
这是 gdb(在命名空间 SLog 中)
0x00005555555b2a1a in std::_Hashtable<SLog::Config, SLog::Config, std::allocator<SLog::Config>, std::__detail::_Identity, std::equal_to<SLog::Config>, std::hash<SLog::Config>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, true, true> >::_M_bucket_index (this=0x0, __k=@0x7fffffffdbb4: SLog::NO_NEW_LINE, __c=0) at /usr/include/c++/7/bits/hashtable.h:631
631 { return __hash_code_base::_M_bucket_index(__k, __c, _M_bucket_count); }
最佳答案
你的 shared_ptr 不管理任何东西。它相当于:
std::unordered_set<Config>* configurations;
在旧世界。
您可以通过以下方式轻松修复它:
configurations.reset(new std::unordered_set<Config>);
或者按照@user4581301 的建议:
auto configurations = std::make_shared<std::unordered_set<Config>>();
关于c++ - 枚举的 std::unordered_set,调用 find 给出段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57651712/
我是一名优秀的程序员,十分优秀!