Erase-remove idiom with std::set failing with constness-related error(带有 std::set 的擦除删除习语失败并出现与 constness 相关的错误)
问题描述
有人可以帮我吗?
编译这段代码:
void test()
{
std::set<int> test;
test.insert(42);
test.erase(std::remove(test.begin(), test.end(), 30), test.end()); // <- Line 33
}
编译时出现如下错误:
$ make
g++ -c -Wall -pedantic-errors -Wextra -Wunused -Werror a_star.cpp
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algo.h: In function `_FIter std::remove(_FIter, _FIter, const _Tp&) [with _FIter = std::_Rb_tree_const_iterator<int>, _Tp = int]':
a_star.cpp:33: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algo.h:779: error: assignment of read-only location `__result.std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = int]()'
make: *** [a_star.o] Error 1
推荐答案
在 std::set
中,元素不可修改.因此,std::set::iterator
也是不可修改的.来自 this 教程,第 27.3.2.1 节:
In std::set
, the elements are not modifiable. So, the std::set::iterator
is also unmodifiable. From this tutorial, section 27.3.2.1:
在简单的关联容器中,其中元素是键,元素是完全不可变的;这嵌套类型迭代器和因此 const_iterator 是相同的.
In simple associative containers, where the elements are the keys, the elements are completely immutable; the nested types iterator and const_iterator are therefore the same.
因此,erase-remove
习语不能按原样应用.您必须编写一个 for
循环,并在其中使用成员函数 std::set::erase
.请参阅此 问题 并接受此 answer 和另一个 answer 了解详细信息,但简而言之,循环如下所示
Hence, the erase-remove
idiom cannot be applied as is. You have to write a for
loop, and use the member function std::set::erase
inside it. See this question and this accepted answer and another answer for exact details, but in short, the loop is like the following
typename std::set::iterator set_iter;
for( set_iter it = s.begin(); it != s.end(); /* blank */ ) {
if( some_condition() ) {
s.erase( it++ ); // Note the subtlety here
}
else {
++it;
}
}
这篇关于带有 std::set 的擦除删除习语失败并出现与 constness 相关的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 std::set 的擦除删除习语失败并出现与 constness 相关的错误
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- C++ 协变模板 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01