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

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

        STL 删除没有按预期工作?

        时间:2024-05-12
          <tbody id='2Rpt5'></tbody>
        • <bdo id='2Rpt5'></bdo><ul id='2Rpt5'></ul>

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

            <small id='2Rpt5'></small><noframes id='2Rpt5'>

                <tfoot id='2Rpt5'></tfoot>

                • <legend id='2Rpt5'><style id='2Rpt5'><dir id='2Rpt5'><q id='2Rpt5'></q></dir></style></legend>
                • 本文介绍了STL 删除没有按预期工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  int main()
                  {
                  
                          const int SIZE = 10;
                          int a[SIZE] = {10, 2, 35, 5, 10, 26, 67, 2, 5, 10};
                          std::ostream_iterator< int > output(cout, " ");
                          std::vector< int > v(a, a + SIZE);
                          std::vector< int >::iterator newLastElement;
                  
                          cout << "contents of the vector: ";
                          std::copy(v.begin(), v.end(), output);
                  
                          newLastElement = std::remove(v.begin(), v.end(), 10);
                          cout << "
                  contents of the vector after remove: ";
                          //std::copy(v.begin(), newLastElement, output); 
                                           //this gives the correct result : 2 35 5 26 67 2 5
                          std::copy(v.begin(), v.end(), output);
                            //this gives a 10 which was supposed to be removed : 2 35 5 26 67 2 5 2 5 10
                  
                          cout << endl;
                          return 0;
                  }
                  

                  数组a中有3个10.

                  为什么我们用remove函数删除了所有的10之后数组v包含一个10.

                  why does the array v contains a 10 after we remove the all the 10s with remove function.

                  你也可以在这里

                  推荐答案

                  实际上 std::remove 不会从容器中删除项目.引用自此处

                  Actually std::remove doesn't remove the item from the container. Quoted from here

                  Remove 从范围 [first, last) 中删除所有等于 value 的元素.也就是说,remove 返回一个迭代器 new_last,使得范围 [first, new_last) 不包含等于 value 的元素.[new_last, last) 范围内的迭代器都仍然可解引用,但它们指向的元素未指定. remove 是稳定的,意味着不等于 value 的元素的相对顺序是不变的.`

                  Remove removes from the range [first, last) all elements that are equal to value. That is, remove returns an iterator new_last such that the range [first, new_last) contains no elements equal to value. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. Remove is stable, meaning that the relative order of elements that are not equal to value is unchanged.`

                  也就是说,std::remove 仅适用于一对迭代器,并且对实际包含项目的容器一无所知.实际上,std::remove 不可能知道底层容器,因为它无法从一对迭代器中发现迭代器所属的容器.所以std::remove 并没有真正删除项目,只是因为它不能.实际从容器中删除项目的唯一方法是调用该容器上的成员函数.

                  That is, std::remove works with a pair of iterators only and does not know anything about the container which actually contains the items. In fact, it's not possible for std::remove to know the underlying container, because there is no way it can go from a pair of iterators to discover about the container to which the iterators belong. So std::remove doesn't really remove the items, simply because it cannot. The only way to actually remove an item from a container is to invoke a member function on that container.

                  因此,如果您想删除这些项目,请使用 Erase-Remove成语:

                  So if you want to remove the items, then use Erase-Remove Idiom:

                   v.erase(std::remove(v.begin(), v.end(), 10), v.end()); 
                  

                  <小时>

                  erase-remove idiom 是如此常见和有用是 std::list 添加了另一个名为 list::remove 的成员函数,它产生与 erase-remove 习语相同的效果.


                  The erase-remove idiom is so common and useful is that std::list has added another member function called list::remove which produces the same effect as that of the erase-remove idiom.

                   std::list<int> l;
                   //...
                   l.remove(10); //it "actually" removes all elements with value 10!
                  

                  这意味着,当您使用 std::list 时,您不需要使用 erase-remove 习惯用法.可以直接调用它的成员函数list::remove.

                  That means, you don't need to use erase-remove idiom when you work with std::list. You can directly call its member function list::remove.

                  这篇关于STL 删除没有按预期工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std::strings 的容量()、reserve() &amp;调整大小()函数 下一篇:迭代器的默认值是什么?

                  相关文章

                • <tfoot id='8j2VL'></tfoot>

                    <bdo id='8j2VL'></bdo><ul id='8j2VL'></ul>

                    <legend id='8j2VL'><style id='8j2VL'><dir id='8j2VL'><q id='8j2VL'></q></dir></style></legend>
                  1. <small id='8j2VL'></small><noframes id='8j2VL'>

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