使用 STL remove_if 的错误输出

问题描述

我正在尝试使用 std::remove_if 从整数向量中删除元素,但没有获得所需的输出

初始向量成员:{0,1,2,3,4,5,6,7,8,9}

所需输出:{0,9}

实际输出:{0,9,9}

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <functional>

    class Equal
    {
    public:
    Equal(int a): a_(a){}
    bool operator()(int b)
    {
    return a_ == b;
    }
    
    private:
    int a_;
    };
    
    int main()
    {
    std::vector<int> vi{0,9};
    
    std::cout << std::endl;
    std::remove_if(vi.begin(),vi.end(),Equal(1));
    for (const auto &i : vi) std::cout << i << " ";
    return 0;
    }

解决方法

在调用之前和之后的 vector 中有同样多的元素 std::remove_if

移除是通过移动(通过 move 赋值)范围内的元素来完成的,这样不会被移除的元素出现在范围的开头。

... 和 std::remove_if 返回到“已删除”元素开头的迭代器,您可以使用该迭代器实际 erase 元素:参见 Erase–remove idiom

vi.erase(
    std::remove_if(vi.begin(),vi.end(),Equal(1)),// returns iterator
    vi.end()                                        // erase to the end
);

Demo

另请注意,std::vector 在 C++20 中获得了一个新的专用函数,它可以同时执行这两项操作,即 std::erase_if

示例:

std::erase_if(vi,Equal(1));