Are they added copy_if in C ++ 0x?

Very annoying that copy_if not in C ++. Does anyone know if it will be in C ++ 0x?

+7
c ++ algorithm c ++ 11
Apr 27 '09 at 16:41
source share
3 answers

Since C ++ 0x is not finalized yet, you can only see the latest project .

+7
Apr 27 '09 at 16:55
source share

At the same time, it’s not very difficult 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 provide a nested type, argument_type , identifying the type of argument β€” as shown above, one convenient way to do this is to infer from unary_function<T, U> , where T is the type of the argument.

+5
Apr 28 '09 at 10:24
source share

Just for completeness, if someone goes this route to this question, it should be mentioned that now (in C ++ 11 and later) there is a copy if . It behaves as expected (copies 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); }); 
+4
May 10 '14 at 9:36
source share



All Articles