擦除删除成语与 std::set 失败并出现与常量相关的错误

2022-01-07 00:00:00 set c++ stl erase

有人可以帮我吗?

编译此代码:

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 也是不可修改的.来自本教程,第 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;
    }
}

相关文章