c++ - Unordered_Set的迭代器抛出奇怪的错误

标签 c++ c++11 iterator unordered-set

我需要浏览一下 map ,并获取一组在下次迭代之前必须删除的条目。我试图使用迭代器的unordered_set将迭代器存储到这些条目。

unordered_set < map<int,int>::iterator > todel;
我得到以下 super 奇怪和长错误。
有人可以告诉我发生了什么事以及如何纠正它?
Line 22: Char 50: fatal error: call to implicitly-deleted default constructor of 'unordered_set<map<int, int>::iterator>' (aka 'unordered_set<_Rb_tree_iterator<std::pair<const int, int> > >')
        unordered_set < map<int,int>::iterator > todel;
                                                 ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/unordered_set.h:135:7: note: explicitly defaulted function was implicitly deleted here
      unordered_set() = default;
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/unordered_set.h:100:18: note: default constructor of 'unordered_set<std::_Rb_tree_iterator<std::pair<const int, int> >, std::hash<std::_Rb_tree_iterator<std::pair<const int, int> > >, std::equal_to<std::_Rb_tree_iterator<std::pair<const int, int> > >, std::allocator<std::_Rb_tree_iterator<std::pair<const int, int> > > >' is implicitly deleted because field '_M_h' has a deleted default constructor
      _Hashtable _M_h;
                 ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/hashtable.h:408:7: note: explicitly defaulted function was implicitly deleted here
      _Hashtable() = default;
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/hashtable.h:174:7: note: default constructor of '_Hashtable<std::_Rb_tree_iterator<std::pair<const int, int> >, std::_Rb_tree_iterator<std::pair<const int, int> >, std::allocator<std::_Rb_tree_iterator<std::pair<const int, int> > >, std::__detail::_Identity, std::equal_to<std::_Rb_tree_iterator<std::pair<const int, int> > >, std::hash<std::_Rb_tree_iterator<std::pair<const int, int> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, true, true> >' is implicitly deleted because base class '__detail::_Hashtable_base<_Rb_tree_iterator<pair<const int, int> >, _Rb_tree_iterator<pair<const int, int> >, _Identity, equal_to<_Rb_tree_iterator<pair<const int, int> > >, hash<_Rb_tree_iterator<pair<const int, int> > >, _Mod_range_hashing, _Default_ranged_hash, _Hashtable_traits<true, true, true> >' has a deleted default constructor
    : public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/hashtable_policy.h:1824:5: note: explicitly defaulted function was implicitly deleted here
    _Hashtable_base() = default;
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/hashtable_policy.h:1773:5: note: default constructor of '_Hashtable_base<std::_Rb_tree_iterator<std::pair<const int, int> >, std::_Rb_tree_iterator<std::pair<const int, int> >, std::__detail::_Identity, std::equal_to<std::_Rb_tree_iterator<std::pair<const int, int> > >, std::hash<std::_Rb_tree_iterator<std::pair<const int, int> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, true, true> >' is implicitly deleted because base class '_Hash_code_base<std::_Rb_tree_iterator<std::pair<const int, int> >, std::_Rb_tree_iterator<std::pair<const int, int> >, std::__detail::_Identity, std::hash<std::_Rb_tree_iterator<std::pair<const int, int> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, _Hashtable_traits<true, true, true>::__hash_cached::value>' has a deleted default constructor
  : public _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/hashtable_policy.h:1379:7: note: explicitly defaulted function was implicitly deleted here
      _Hash_code_base() = default;
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/hashtable_policy.h:1355:7: note: default constructor of '_Hash_code_base<std::_Rb_tree_iterator<std::pair<const int, int> >, std::_Rb_tree_iterator<std::pair<const int, int> >, std::__detail::_Identity, std::hash<std::_Rb_tree_iterator<std::pair<const int, int> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>' is implicitly deleted because base class '_Hashtable_ebo_helper<1, std::hash<std::_Rb_tree_iterator<std::pair<const int, int> > > >' has a deleted default constructor
      private _Hashtable_ebo_helper<1, _H1>,
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/hashtable_policy.h:1106:7: note: explicitly defaulted function was implicitly deleted here
      _Hashtable_ebo_helper() = default;
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/hashtable_policy.h:1104:7: note: default constructor of '_Hashtable_ebo_helper<1, std::hash<std::_Rb_tree_iterator<std::pair<const int, int> > >, true>' is implicitly deleted because base class 'std::hash<std::_Rb_tree_iterator<std::pair<const int, int> > >' has a deleted default constructor
    : private _Tp
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/functional_hash.h:101:19: note: default constructor of 'hash<std::_Rb_tree_iterator<std::pair<const int, int> > >' is implicitly deleted because base class '__hash_enum<std::_Rb_tree_iterator<std::pair<const int, int> > >' has no default constructor
    struct hash : __hash_enum<_Tp>
                  ^
1 error generated.

最佳答案

std::unordered_set<T>要求T是可哈希的,而std::map<K,V>::iterator则不是。您可以为此原因使用std::set<T>,而这需要T具有比较operator<

关于c++ - Unordered_Set的迭代器抛出奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62711460/

相关文章:

c++ - 我如何告诉 clang-format 缩进可见性修饰符?

c++ - 必须在模板化类上调用对非静态成员函数的引用

c++ - vector 迭代器 -> 打印类(缺少参数)

c++ - -O3(优化级别 3)有什么问题?

C++ 多回调函数

c++ - GCC:如何只生成行号调试信息?

C++检测模板类

c++ - 遍历集合

c++ - 在这里进行 const 转换安全吗?

C++:宏扩展解释