• <bdo id='9Npea'></bdo><ul id='9Npea'></ul>

    <small id='9Npea'></small><noframes id='9Npea'>

    1. <tfoot id='9Npea'></tfoot>
      <legend id='9Npea'><style id='9Npea'><dir id='9Npea'><q id='9Npea'></q></dir></style></legend>
    2. <i id='9Npea'><tr id='9Npea'><dt id='9Npea'><q id='9Npea'><span id='9Npea'><b id='9Npea'><form id='9Npea'><ins id='9Npea'></ins><ul id='9Npea'></ul><sub id='9Npea'></sub></form><legend id='9Npea'></legend><bdo id='9Npea'><pre id='9Npea'><center id='9Npea'></center></pre></bdo></b><th id='9Npea'></th></span></q></dt></tr></i><div id='9Npea'><tfoot id='9Npea'></tfoot><dl id='9Npea'><fieldset id='9Npea'></fieldset></dl></div>
    3. 在迭代时从地图(或任何其他 STL 容器)中擦除/删除内容

      Erase/Remove contents from the map (or any other STL container) while iterating it(在迭代时从地图(或任何其他 STL 容器)中擦除/删除内容)
        <bdo id='xSVPZ'></bdo><ul id='xSVPZ'></ul>

              <tbody id='xSVPZ'></tbody>
          1. <small id='xSVPZ'></small><noframes id='xSVPZ'>

            <legend id='xSVPZ'><style id='xSVPZ'><dir id='xSVPZ'><q id='xSVPZ'></q></dir></style></legend>

            <i id='xSVPZ'><tr id='xSVPZ'><dt id='xSVPZ'><q id='xSVPZ'><span id='xSVPZ'><b id='xSVPZ'><form id='xSVPZ'><ins id='xSVPZ'></ins><ul id='xSVPZ'></ul><sub id='xSVPZ'></sub></form><legend id='xSVPZ'></legend><bdo id='xSVPZ'><pre id='xSVPZ'><center id='xSVPZ'></center></pre></bdo></b><th id='xSVPZ'></th></span></q></dt></tr></i><div id='xSVPZ'><tfoot id='xSVPZ'></tfoot><dl id='xSVPZ'><fieldset id='xSVPZ'></fieldset></dl></div>
              <tfoot id='xSVPZ'></tfoot>
              • 本文介绍了在迭代时从地图(或任何其他 STL 容器)中擦除/删除内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                据称,您不能在迭代时擦除/删除容器中的元素,因为迭代器变得无效.删除满足特定条件的元素的(安全)方法是什么?请只使用 stl,不要使用 boost 或 tr1.

                Allegedly you cannot just erase/remove an element in a container while iterating as iterator becomes invalid. What are the (safe) ways to remove the elements that meet a certain condition? please only stl, no boost or tr1.

                编辑如果我想擦除一些满足特定条件的元素,是否有更优雅的方法,也许使用函子和 for_each 或擦除算法?

                EDIT Is there a more elegant way if I want to erase a number of elements that meet a certain criteria, perhaps with using functor and for_each or erase algorithm ?

                推荐答案

                bool IsOdd( int i )
                {
                    return (i&1)!=0;
                }
                
                int a[] = {1,2,3,4,5};
                vector<int> v( a, a + 5 );
                v.erase( remove_if( v.begin(), v.end(), bind1st( equal_to<int>(), 4 ) ), v.end() );
                // v contains {1,2,3,5}
                v.erase( remove_if( v.begin(), v.end(), IsOdd ), v.end() );
                // v contains {2}
                

                这篇关于在迭代时从地图(或任何其他 STL 容器)中擦除/删除内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                Adding types to the std namespace(将类型添加到 std 命名空间)
                Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)

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

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

                          <legend id='DgwKZ'><style id='DgwKZ'><dir id='DgwKZ'><q id='DgwKZ'></q></dir></style></legend>

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