are they adding copy_if to c++0x?

are they adding copy_if to c++0x?

爱的故事 发布于 2021-11-24 字数 107 浏览 981 回复 3 原文

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

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转角预定愛 2022-06-07 3 楼

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<int> foo{ 25, 15, 5, -5, -15 };
std::vector<int> bar;

// copy only positive numbers:
auto it = std::copy_if (foo.begin(), foo.end(), std::back_inserter(bar), 
            [](int i){return !(i<0);
          });
格子衫的從容 2022-06-07 2 楼

In the meantime, it's not very hard to make your own copy_if() using remove_copy_if():

#include <functional>

struct my_predicate : std::unary_function<my_arg_type, bool> {
    bool operator()(my_arg_type const& x) const { ... }
};

// To perform "copy_if(x, y, z, my_predicate())", write:
remove_copy_if(x, y, z, std::not1(my_predicate()));

Using not1() requires your predicate class to supply a nested type, argument_type, identifying the type of the argument -- as shown above, one convenient way to do this is to derive from unary_function<T, U>, where T is the argument type.

不醒的梦 2022-06-07 1 楼

Since the C++0x is not yet finalized, you can only take a look at the most recent draft.