嗨,我正在尝试将集合的特定节点添加到列表中,该列表位于哈希表中

Hi, I am trying to add specific nodes of a set to a list, the list being in a hash table

本文关键字:列表 节点 添加 哈希表 集合      更新时间:2023-10-16

我有以下集合和列表声明:

set<mystruct> my_entries;
unordered_map<string, list<_Rb_tree_const_iterator<mystruct>>> mylist;

我正在尝试做的基本上是在将集合的一些特定节点插入到集合中后将其插入到列表中。

auto inserted = my_entries.insert(entry);
mylist[name].push_back(*inserted.first);

我收到以下错误:

error: no matching function for call to 
'std::list<std::_Rb_tree_const_iterator<mystruct> >::push_back(const 
mystruct&)’
     mylist[name].push_back(*inserted.first);
note:   no known conversion for argument 1 from 'const mylist’ to 
'std::list<std::_Rb_tree_const_iterator<mylist> >::value_type&& {aka 
std::_Rb_tree_const_iterator<mylist>&&}'

知道出了什么问题吗?

谢谢。

如注释所述,不应尝试使用标准库中的内部类型。 您可以按如下方式存储迭代器:

set<mystruct> my_entries;
unordered_map<string, list<set<mystruct>::const_iterator>> mylist;
// ...
mylist[name].push_back(inserted.first);

每当存储迭代器时,了解管理此类迭代器何时变得无效的规则非常重要。 在 std::set::insert 的情况下,您可以保证:

没有迭代器或引用失效。