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

      2. <tfoot id='FgCbr'></tfoot>

        C++ 按值而不是按位置擦除向量元素?

        时间:2023-09-16

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

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

                    <tbody id='ZVc0M'></tbody>
                  本文介绍了C++ 按值而不是按位置擦除向量元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  vector<int> myVector;
                  

                  让我们说向量中的值是这个(按这个顺序):

                  and lets say the values in the vector are this (in this order):

                  5 9 2 8 0 7
                  

                  如果我想删除包含值8"的元素,我想我会这样做:

                  If I wanted to erase the element that contains the value of "8", I think I would do this:

                  myVector.erase(myVector.begin()+4);
                  

                  因为这会擦除第 4 个元素.但是有没有办法根据值8"擦除元素?喜欢:

                  Because that would erase the 4th element. But is there any way to erase an element based off of the value "8"? Like:

                  myVector.eraseElementWhoseValueIs(8);
                  

                  还是我只需要遍历所有向量元素并测试它们的值?

                  Or do I simply just need to iterate through all the vector elements and test their values?

                  推荐答案

                  std::remove() 代替:

                  #include <algorithm>
                  ...
                  vec.erase(std::remove(vec.begin(), vec.end(), 8), vec.end());
                  

                  这种组合也称为擦除-删除习语.

                  这篇关于C++ 按值而不是按位置擦除向量元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:c ++ 矢量大小.为什么 -1 大于零 下一篇:转换向量<int>到一个字符串

                  相关文章

                  最新文章

                1. <small id='JvHyO'></small><noframes id='JvHyO'>

                    <tfoot id='JvHyO'></tfoot>

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