擦除删除成语与 std::set 失败并出现与常量相关的错误 [英] Erase-remove idiom with std::set failing with constness-related error

查看:24
本文介绍了擦除删除成语与 std::set 失败并出现与常量相关的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗?

编译此代码:

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;
    }
}

这篇关于擦除删除成语与 std::set 失败并出现与常量相关的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆