指针存储在std :: set const中吗?

use*_*947 0 c++ iterator stl const set

我正在搜索代码中的错误,我有一个问题:

 class a
 {
 public:
 void foo(int a) {}
 }

  std::set<a*> set;
  std::set<a*>::iterator it = set.begin();

  it->foo(55); //gives me error:
  // error: request for member ‘foo’ in ‘* it.std::_Rb_tree_const_iterator<_Tp>::operator-><a*>()’, which is of pointer type ‘a* const’ (maybe you meant to use ‘->’ ?)
Run Code Online (Sandbox Code Playgroud)

为什么它不允许我使用非const函数呢?如果不使用强制转换,我可以做一组非常量指针?

Kon*_*lph 9

你需要解除引用两次:

(*it)->foo(55);
Run Code Online (Sandbox Code Playgroud)

- it是指针的迭代器.如果你有一个代码,那么你的代码是正确std::set<a>std::set<a*>.