are they adding copy_if to c++0x?

后端 未结 3 1046
一整个雨季
一整个雨季 2021-01-02 18:27

It\'s very annoying that copy_if is not in C++. Does anyone know if it will be in C++0x?

3条回答
  •  长发绾君心
    2021-01-02 19:09

    Just for completeness, in case someone googles his/her way to this question, it should be mentioned that now (in C++11 and later) there is a copy if algorithm. It behaves as expected (copies the elements in a range, for which some predicate returns true, to another range).

    A typical use case would be

    std::vector foo{ 25, 15, 5, -5, -15 };
    std::vector bar;
    
    // copy only positive numbers:
    auto it = std::copy_if (foo.begin(), foo.end(), std::back_inserter(bar), 
                [](int i){return !(i<0);
              });
    

提交回复
热议问题