• <legend id='umd0j'><style id='umd0j'><dir id='umd0j'><q id='umd0j'></q></dir></style></legend>

        <small id='umd0j'></small><noframes id='umd0j'>

        <i id='umd0j'><tr id='umd0j'><dt id='umd0j'><q id='umd0j'><span id='umd0j'><b id='umd0j'><form id='umd0j'><ins id='umd0j'></ins><ul id='umd0j'></ul><sub id='umd0j'></sub></form><legend id='umd0j'></legend><bdo id='umd0j'><pre id='umd0j'><center id='umd0j'></center></pre></bdo></b><th id='umd0j'></th></span></q></dt></tr></i><div id='umd0j'><tfoot id='umd0j'></tfoot><dl id='umd0j'><fieldset id='umd0j'></fieldset></dl></div>
          <bdo id='umd0j'></bdo><ul id='umd0j'></ul>
        <tfoot id='umd0j'></tfoot>
      1. 擦除删除成语与 std::set 失败并出现与常量相关的错误

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

          <tfoot id='EIPG2'></tfoot>

          <small id='EIPG2'></small><noframes id='EIPG2'>

              • <bdo id='EIPG2'></bdo><ul id='EIPG2'></ul>

              • <i id='EIPG2'><tr id='EIPG2'><dt id='EIPG2'><q id='EIPG2'><span id='EIPG2'><b id='EIPG2'><form id='EIPG2'><ins id='EIPG2'></ins><ul id='EIPG2'></ul><sub id='EIPG2'></sub></form><legend id='EIPG2'></legend><bdo id='EIPG2'><pre id='EIPG2'><center id='EIPG2'></center></pre></bdo></b><th id='EIPG2'></th></span></q></dt></tr></i><div id='EIPG2'><tfoot id='EIPG2'></tfoot><dl id='EIPG2'><fieldset id='EIPG2'></fieldset></dl></div>
                  <legend id='EIPG2'><style id='EIPG2'><dir id='EIPG2'><q id='EIPG2'></q></dir></style></legend>

                    <tbody id='EIPG2'></tbody>

                  本文介绍了擦除删除成语与 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 失败并出现与常量相关的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                  <i id='Zsyn0'><tr id='Zsyn0'><dt id='Zsyn0'><q id='Zsyn0'><span id='Zsyn0'><b id='Zsyn0'><form id='Zsyn0'><ins id='Zsyn0'></ins><ul id='Zsyn0'></ul><sub id='Zsyn0'></sub></form><legend id='Zsyn0'></legend><bdo id='Zsyn0'><pre id='Zsyn0'><center id='Zsyn0'></center></pre></bdo></b><th id='Zsyn0'></th></span></q></dt></tr></i><div id='Zsyn0'><tfoot id='Zsyn0'></tfoot><dl id='Zsyn0'><fieldset id='Zsyn0'></fieldset></dl></div>
                    <bdo id='Zsyn0'></bdo><ul id='Zsyn0'></ul>

                        <tbody id='Zsyn0'></tbody>
                          <legend id='Zsyn0'><style id='Zsyn0'><dir id='Zsyn0'><q id='Zsyn0'></q></dir></style></legend>

                          <small id='Zsyn0'></small><noframes id='Zsyn0'>

                          1. <tfoot id='Zsyn0'></tfoot>