带有 std::set 的擦除删除习语失败并出现与 constness 相关的错误

Erase-remove idiom with std::set failing with constness-related error(带有 std::set 的擦除删除习语失败并出现与 constness 相关的错误)

本文介绍了带有 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 相关的错误

基础教程推荐