c++ - 为什么这个迭代器是常量的?

标签 c++ stl

<分区>

Possible Duplicate:
C++ STL set update is tedious: I can't change an element in place

为什么这段代码提示我的参数是一个 const_iterator 而不是这样声明的?

void foo(std::set<int>::iterator it)
{
  *it=2;
}

我收到错误:分配只读位置‘it.std::_Rb_tree_const_iterator<_Tp>::operator* with _Tp = int’

最佳答案

因为它是一个集合。集合使用 const 迭代器,因为您无法在不弄乱集合顺序的情况下更改值。

关于c++ - 为什么这个迭代器是常量的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9709655/

相关文章:

c++ - 获取指向 std::list 或 std::forward_list 中节点的指针

c++ - 查找字符串可以减少为0的步骤数

c++ - Windows 上 OpenCL 库的 0xc000007b 加载时错误

c++ - 使用 c_str() 返回的指针删除动态分配的 std::string 是否会导致 C++ 中的内存泄漏?

c++ - 如何在 vector<vector<pair<int,int>> 中插入一个 vpii >

c++ - 无法将 Boost 路径迭代器转换为字符串

c++ - std::vector 构造,具有元素的就地构造

c++ - Makefile,在src目录树中查找源代码并在构建文件中编译为.o

c++ - 似乎无法在结构中使用整数

c++ - 使用 unique_ptr 成员编写移动构造函数的正确方法(崩溃)