c++ - 指针是否存储在 std::set const 中?

标签 c++ stl iterator set constants

我正在寻找代码中的错误,但遇到了一个问题:

 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 ‘->’ ?)

为什么它不允许我在上面使用非常量函数?如何在不使用强制转换的情况下拥有一组非常量指针?

最佳答案

您需要取消引用两次:

(*it)->foo(55);

it是指针的迭代器。如果您有 std::set<a>,您的代码将是正确的而不是 std::set<a*> .

关于c++ - 指针是否存储在 std::set const 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14606637/

相关文章:

c++ - std::map::size_type 对于 std::map 其 value_type 是它自己的 size_type

Rust 从迭代器中收集 HashMap

c++ - 在没有特定 IDE 的 Windows 7 中使用 Rcpp 和 RInside

c++ - OOP 未在此范围内声明

c++ - 使用 unordered_map 分配器的非默认构造函数

c++ - 无法在 MSVC10 中使用 SFINAE、类型特征和 static_assert

c++ - 将 std::stringstream 数据高效解析为 std::vector<std::vector<double>>

c++ - 有没有办法访问 STL 容器适配器的底层容器?

c++ - 取消引用指针列表的迭代器

java - 如何使用迭代器以编程方式绘制 N 个元素的 block