c++ - unordered_set 通过地址传递

标签 c++ stl unordered-set

我已经从 C 转向 C++,并且最近学习了 STL。

最后一行在 STL 样式中给出了很长的错误(无助)或者也许我是模板的新手,这就是为什么我觉得它无能为力。

int insert(Forest *forest, int e1) {
    Forest::const_iterator te1;
    te1 = forest->begin();
    te1->insert(e1);
}
int main() {
    //some code here
    Forest forest = (5, myHash);
    insert(&forest, e1);
}

森林是:

typedef unordered_set<unordered_set<int>, function<size_t(const unordered_set<int>)>> Forest;

编辑: 尝试作为建议的答案之一后

Forest::iterator te1 = forest->begin();
te1->insert(e1);

它仍然给出相同的错误。

最佳答案

在 C++11 中,std::setstd::unordered_set 中的项都是 const。您不能插入其中之一,必须将其删除并重新添加。

这是因为您要通过其哈希值确定父集合中的位置,如果您向其中添加新项目,该位置可能会发生变化。它类似于 std::map 中的键是 const

关于c++ - unordered_set 通过地址传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19348567/

相关文章:

c++ - 使用大线程池调试多线程 C++ 应用程序

c++ - 关于 C++ 宏多态性

c++ - 我如何使用微软的 C++ 分配器

c++ - 无序设置为 boost::asio::ip::tcp::endpoint - 试图引用已删除的函数

c++ - STL 无序容器的局部迭代器有哪些用途?

c++ - 如何为用户定义的类型专门化 std::hash<T>?

java - 对于想要快速入门的 Java 程序员来说,哪些 Visual C++ 引用资料值得一看?

c++ - 如何访问线程外的线程数据

c++ - map operator[] 的返回值(和 "at"方法)

c++ - SFINAE应用于迭代器