std::experimental::ranges::copy, std::experimental::ranges::copy_if
Defined in header <experimental/ranges/algorithm>
|
||
template < ranges::InputIterator I, ranges::Sentinel<I> S, ranges::WeaklyIncrementable O > requires ranges::IndirectlyCopyable<I, O>() |
(1) | (ranges TS) |
template < ranges::InputRange Rng, ranges::WeaklyIncrementable O > requires ranges::IndirectlyCopyable<ranges::iterator_t<Rng>, O>() |
(2) | (ranges TS) |
template < ranges::InputIterator I, ranges::Sentinel<I> S, ranges::WeaklyIncrementable O, class Proj = ranges::identity, |
(3) | (ranges TS) |
template < ranges::InputRange Rng, ranges::WeaklyIncrementable O, class Proj = ranges::identity, |
(4) | (ranges TS) |
Copies elements in the source range ([first,last)
or rng
) into the destination range beginning at result
, starting from the first element in the source range and proceeding to the last one.
[first, last)
. For each non-negative integer n < (last - first)
, performs *(result + n) = *(first + n). The behavior is undefined if result
is within the range [first, last)
. In this case, ranges::copy_backward may be used instead.rng
as the source range, as if by ranges::copy(ranges::begin(rng), ranges::end(rng), result); except that result
may not be copied.pred
returns true when applied to the element's value as projected by the projection proj
. The order of the elements that are copied is preserved. The behavior is undefined if the source and the destination ranges overlap.rng
as the source range, as if by ranges::copy_if(ranges::begin(rng), ranges::end(rng), result, pred, proj); except that result
, pred
and proj
may not be copied.Parameters
first, last | - | the range of elements to copy |
rng | - | the range of elements to copy |
result | - | the beginning of the destination range |
pred | - | predicate to apply to the projected elements |
proj | - | projection to apply to the elements |
Return value
A tagged_pair
object containing the following two members:
- The first member, with the tag
tag::in
, is the past-the-end iterator of the source range (that is, an iterator of typeI
that compares equal to the sentinellast
). - The second member, with the tag
tag::out
, is the past-the-end iterator of the result range.
Complexity
Possible implementations
First version |
---|
template <ranges::InputIterator I, ranges::Sentinel<I> S, ranges::WeaklyIncrementable O> requires ranges::IndirectlyCopyable<I, O>() ranges::tagged_pair<ranges::tag::in(I), ranges::tag::out(O)> copy(I first, S last, O result) { for (; first != last; ++first, (void)++result) *result = *first; return {first, result}; } |
Second version |
template <ranges::InputRange Rng, ranges::WeaklyIncrementable O> requires ranges::IndirectlyCopyable<ranges::iterator_t<Rng>, O>() ranges::tagged_pair<ranges::tag::in(ranges::safe_iterator_t<Rng>), ranges::tag::out(O)> copy(Rng&& rng, O result) { return ranges::copy(ranges::begin(rng), ranges::end(rng), result); } |
Third version |
template <ranges::InputIterator I, ranges::Sentinel<I> S, ranges::WeaklyIncrementable O, class Proj = ranges::identity, ranges::IndirectPredicate<ranges::projected<I, Proj>> Pred> requires ranges::IndirectlyCopyable<I, O>() ranges::tagged_pair<ranges::tag::in(I), ranges::tag::out(O)> copy_if(I first, S last, O result, Pred pred, Proj proj = Proj{}) { for (; first != last; ++first) { if (ranges::invoke(pred, ranges::invoke(proj, *first))){ *result = *first; ++result; } } return {first, result}; } |
Fourth version |
template <ranges::InputRange Rng, ranges::WeaklyIncrementable O, class Proj = ranges::identity, ranges::IndirectPredicate<ranges::projected<ranges::iterator_t<Rng>, Proj>> Pred> requires ranges::IndirectlyCopyable<ranges::iterator_t<Rng>, O>() ranges::tagged_pair<ranges::tag::in(ranges::safe_iterator_t<Rng>), ranges::tag::out(O)> copy_if(Rng&& rng, O result, Pred pred, Proj proj = Proj{}) { return ranges::copy_if(ranges::begin(rng), ranges::end(rng), result, pred, proj); } |
Example
The following code uses copy to both copy the contents of one vector to another and to display the resulting vector:
#include <experimental/ranges/algorithm> #include <iostream> #include <vector> #include <experimental/ranges/iterator> #include <numeric> int main() { // see http://en.cppreference.com/w/cpp/language/namespace_alias namespace ranges = std::experimental::ranges; std::vector<int> from_vector(10); std::iota(from_vector.begin(), from_vector.end(), 0); std::vector<int> to_vector; ranges::copy_if(from_vector.begin(), from_vector.end(), ranges::back_inserter(to_vector), [](const auto i) { return i % 3; }); // or, alternatively, // std::vector<int> to_vector(from_vector.size()); // std::copy(from_vector, to_vector.begin()); std::cout << "to_vector contains: "; ranges::copy(to_vector, ranges::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; }
Output:
to_vector contains: 1 2 4 5 7 8
See also
(C++11) |
copies a range of elements to a new location (function template) |
copies a range of elements in backwards order (function template) | |
creates a copy of a range that is reversed (function template) | |
copies a number of elements to a new location (function template) | |
assigns a range of elements a certain value (function template) | |
copies a range of elements omitting those that satisfy specific criteria (function template) |