Is there a better alternative to std::remove_if to remove elements from a vector?

后端 未结 2 2040
梦谈多话
梦谈多话 2020-12-09 15:33

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 l

2条回答
  •  长情又很酷
    2020-12-09 15:59

    This will become available in a C++17-ready compiler soon through the std::experimental::erase_if algorithm:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        std::vector ints { -1, 0, 1 };   
        std::experimental::erase_if(ints, [](int x){
            return x < 0;
        });
        std::copy(ints.begin(), ints.end(), std::ostream_iterator(std::cout, ","));
    }
    

    Live Example that prints 0,1

提交回复
热议问题