用std :: set擦除删除习惯用与constness相关的错误失败

Jar*_*red 17 c++ stl set erase

有人可以帮帮我吗?

编译此代码:

void test()
{
  std::set<int> test;
  test.insert(42);
  test.erase(std::remove(test.begin(), test.end(), 30), test.end());  // <- Line 33
}
Run Code Online (Sandbox Code Playgroud)

编译时生成以下错误:

$ 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
Run Code Online (Sandbox Code Playgroud)

Aru*_*run 25

std::set,元素是不可修改的.所以,这std::set::iterator也是不可修改的.从教程的第27.3.2.1节:

在简单的关联容器中,元素是键,元素是完全不可变的; 嵌套类型iterator和const_iterator因此是相同的.

因此,erase-remove成语不能按原样应用.你必须编写一个for循环,并使用其中的成员函数std::set::erase.看到这个问题和这个接受的答案和另一个答案的确切细节,但简而言之,循环如下

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


AnT*_*AnT 5

擦除删除习惯用法不能与关联容器一起使用.关联容器不允许通过迭代器修改整个容器元素,这意味着std::remove不能对它们应用变异序列操作(如).