我已经从 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::set
或 std::unordered_set
中的项都是 const
。您不能插入其中之一,必须将其删除并重新添加。
这是因为您要通过其哈希值确定父集合中的位置,如果您向其中添加新项目,该位置可能会发生变化。它类似于 std::map
中的键是 const
。
关于c++ - unordered_set 通过地址传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19348567/