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

        <bdo id='pEVT4'></bdo><ul id='pEVT4'></ul>

    1. <tfoot id='pEVT4'></tfoot>
    2. <small id='pEVT4'></small><noframes id='pEVT4'>

      1. <i id='pEVT4'><tr id='pEVT4'><dt id='pEVT4'><q id='pEVT4'><span id='pEVT4'><b id='pEVT4'><form id='pEVT4'><ins id='pEVT4'></ins><ul id='pEVT4'></ul><sub id='pEVT4'></sub></form><legend id='pEVT4'></legend><bdo id='pEVT4'><pre id='pEVT4'><center id='pEVT4'></center></pre></bdo></b><th id='pEVT4'></th></span></q></dt></tr></i><div id='pEVT4'><tfoot id='pEVT4'></tfoot><dl id='pEVT4'><fieldset id='pEVT4'></fieldset></dl></div>
      2. 擦除和删除的区别

        时间:2024-05-12
      3. <i id='fRWmH'><tr id='fRWmH'><dt id='fRWmH'><q id='fRWmH'><span id='fRWmH'><b id='fRWmH'><form id='fRWmH'><ins id='fRWmH'></ins><ul id='fRWmH'></ul><sub id='fRWmH'></sub></form><legend id='fRWmH'></legend><bdo id='fRWmH'><pre id='fRWmH'><center id='fRWmH'></center></pre></bdo></b><th id='fRWmH'></th></span></q></dt></tr></i><div id='fRWmH'><tfoot id='fRWmH'></tfoot><dl id='fRWmH'><fieldset id='fRWmH'></fieldset></dl></div>

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

      4. <legend id='fRWmH'><style id='fRWmH'><dir id='fRWmH'><q id='fRWmH'></q></dir></style></legend>
        <tfoot id='fRWmH'></tfoot>

                <bdo id='fRWmH'></bdo><ul id='fRWmH'></ul>

                    <tbody id='fRWmH'></tbody>
                  本文介绍了擦除和删除的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 std::remove 算法的用法之间的差异感到有些困惑.具体来说,当我使用此算法时,我无法理解正在删除的内容.我写了一个像这样的小测试代码:

                  I am bit confused about the difference between the usage of std::remove algorithm. Specifically I am not able to understand what is being removed when I use this algorithm. I wrote a small test code like this:

                  std::vector<int> a;
                  a.push_back(1);
                  a.push_back(2);
                  
                  std::remove(a.begin(), a.end(), 1);
                  
                  
                  int s = a.size();
                  
                  std::vector<int>::iterator iter = a.begin();
                  std::vector<int>::iterator endIter = a.end();
                  
                  std::cout<<"Using iter...
                  ";
                  for(; iter != endIter; ++iter)
                  {
                      std::cout<<*iter<<"
                  ";
                  }
                  
                  std::cout<<"Using size...
                  ";
                  for(int i = 0; i < a.size(); ++i)
                  {
                      std::cout<<a[i]<<"
                  ";
                  }
                  

                  两种情况下的输出都是 2,2.

                  The output was 2,2 in both the cases.

                  但是,如果我使用擦除和删除这样的东西:

                  However, if I use erase with the remove something like this:

                  a.erase(std::remove(a.begin(), a.end(), 1), a.end());
                  

                  我得到的输出为 2.

                  所以我的问题是:

                  (1).除了将 std::remove 与擦除功能一起使用之外,还有其他用途吗.

                  (1). Is there any use of std::remove other than using it with erase function.

                  (2).即使在执行 std::remove 之后,为什么 a.size() 返回 2 而不是 1?

                  (2). Even after doing std::remove, why a.size() returns 2 and not 1?

                  我阅读了 Scott Meyer 的 Effective STL 书中关于擦除-删除习语的条目.但我仍然有这种困惑.

                  I read the item in Scott Meyer's Effective STL book about the erase-remove idiom. But am still having this confusion.

                  推荐答案

                  remove() 实际上并没有从容器中删除元素——它只会在已删除的元素之上向前分流未删除的元素元素.关键是要意识到remove() 不仅可以用于容器,还可以用于任意任意前向迭代器对:这意味着它不能 实际上删除元素,因为任意迭代器对不一定具有删除元素的能力.

                  remove() doesn't actually delete elements from the container -- it only shunts non-deleted elements forwards on top of deleted elements. The key is to realise that remove() is designed to work on not just a container but on any arbitrary forward iterator pair: that means it can't actually delete the elements, because an arbitrary iterator pair doesn't necessarily have the ability to delete elements.

                  例如,指向常规 C 数组开头和结尾的指针是前向迭代器,因此可以与 remove() 一起使用:

                  For example, pointers to the beginning and end of a regular C array are forward iterators and as such can be used with remove():

                  int foo[100];
                  
                  ...
                  
                  remove(foo, foo + 100, 42);    // Remove all elements equal to 42
                  

                  这里很明显remove()不能调整数组的大小!

                  Here it's obvious that remove() cannot resize the array!

                  这篇关于擦除和删除的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何为我的班级提供交换功能? 下一篇:std:map 中的浮点键

                  相关文章

                • <small id='mQ0Sq'></small><noframes id='mQ0Sq'>

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

                  2. <legend id='mQ0Sq'><style id='mQ0Sq'><dir id='mQ0Sq'><q id='mQ0Sq'></q></dir></style></legend>
                    <tfoot id='mQ0Sq'></tfoot>

                        <bdo id='mQ0Sq'></bdo><ul id='mQ0Sq'></ul>