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

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

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

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

        有没有比 std::remove_if 更好的替代方法来从向量中删除元素?

        Is there a better alternative to std::remove_if to remove elements from a vector?(有没有比 std::remove_if 更好的替代方法来从向量中删除元素?)
          <bdo id='a7ftn'></bdo><ul id='a7ftn'></ul>
            • <i id='a7ftn'><tr id='a7ftn'><dt id='a7ftn'><q id='a7ftn'><span id='a7ftn'><b id='a7ftn'><form id='a7ftn'><ins id='a7ftn'></ins><ul id='a7ftn'></ul><sub id='a7ftn'></sub></form><legend id='a7ftn'></legend><bdo id='a7ftn'><pre id='a7ftn'><center id='a7ftn'></center></pre></bdo></b><th id='a7ftn'></th></span></q></dt></tr></i><div id='a7ftn'><tfoot id='a7ftn'></tfoot><dl id='a7ftn'><fieldset id='a7ftn'></fieldset></dl></div>
              • <legend id='a7ftn'><style id='a7ftn'><dir id='a7ftn'><q id='a7ftn'></q></dir></style></legend>

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

                <tfoot id='a7ftn'></tfoot>

                  <tbody id='a7ftn'></tbody>

                  本文介绍了有没有比 std::remove_if 更好的替代方法来从向量中删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  std::vector 或其他容器中删除具有特定属性的元素的任务有助于实现函数式风格:为什么要为循环、内存释放和正确移动数据而烦恼?

                  The task of removing elements with a certain property from a std::vector or other container lends itself to a functional style implementation: Why bother with loops, memory deallocation and moving data around correctly?

                  但是,在 C++ 中执行此操作的标准方法似乎是以下习语:

                  However the standard way of doing this in C++ seems to be the following idiom:

                  std::vector<int> ints;
                  ...
                  ints.erase(
                      std::remove_if(ints.begin(), 
                                     ints.end(),
                                     [](int x){return x < 0;}),
                      ints.end());
                  

                  此示例从整数向量中删除所有小于零的元素.

                  This example removes all elements less than zero from an integer vector.

                  我发现它不仅丑,而且容易误用.很明显 std::remove_if 不能改变向量的大小(正如它的名字所暗示的那样),因为它只能传递迭代器.但是很多开发者,包括我自己,一开始并没有意识到这一点.

                  I find it not only ugly but also easy to use incorrectly. It is clear that std::remove_if cannot change the size of the vector (as its name would suggest) because it only gets iterators passed. But many developers, including myself, don't get that in the beginning.

                  那么有没有更安全、更优雅的方式来实现这一目标?如果没有,为什么?

                  So is there a safer and hopefully more elegant way to achieve this? If not, why?

                  推荐答案

                  我发现它不仅丑,而且容易误用.

                  I find it not only ugly but also easy to use incorrectly.

                  别担心,我们一开始都做到了.

                  Don't worry, we all did at the start.

                  很明显, std::remove_if 不能改变向量的大小(正如它的名字所暗示的那样),因为它只会让迭代器通过.但是很多开发者,包括我自己,一开始并没有意识到这一点.

                  It is clear that std::remove_if cannot change the size of the vector (as its name would suggest) because it only gets iterators passed. But many developers, including myself, don't get that in the beginning.

                  一样.它让每个人都感到困惑.多年前,它可能不应该被称为 remove_if.事后诸葛亮,嗯?

                  Same. It confuses everyone. It probably shouldn't have been called remove_if all those years ago. Hindsight, eh?

                  那么有没有更安全、更优雅的方式来实现这一目标?

                  So is there a safer and hopefully more elegant way to achieve this?

                  没有

                  如果不是,为什么?

                  因为这是从容器中删除项目时保持性能的最安全、最优雅的方式,其中删除项目会使迭代器失效.

                  Because this is the safest, most elegant way that preserves performance when deleting items from a container in which deleting an item invalidates iterators.

                  期待:

                  有什么我可以做的吗?

                  是的,把这个习语包装成一个函数

                  Yes, wrap this idiom into a function

                  template<class Container, class F>
                  auto erase_where(Container& c, F&& f)
                  {
                      return c.erase(std::remove_if(c.begin(), 
                                                    c.end(),
                                                    std::forward<F>(f)),
                                     c.end());    
                  }
                  

                  激励示例中的调用然后变为:

                  The call in the motivating example then becomes:

                  auto is_negative = [](int x){return x < 0;};
                  erase_where(ints, is_negative);
                  

                  erase_where(ints, [](int x){return x < 0;});
                  

                  这篇关于有没有比 std::remove_if 更好的替代方法来从向量中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中将列表元素移动到末尾)

                        <bdo id='9tQrm'></bdo><ul id='9tQrm'></ul>

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

                            <tfoot id='9tQrm'></tfoot>

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