std::experi...es::copy_if(3)
π NAME π
bestie, welcome to std::experi...es::copy_if(3) from the C++ Standard Libary β yes, thatβs the headerβs spelling, weβre keeping it π
. This page is all about std::experimental::ranges::copy and std::experimental::ranges::copy_if, no cap.
β¨ Synopsis β¨
Defined in header <experimental/ranges/algorithm>.
(1) (ranges TS)
template< InputIterator I, Sentinel<I> S, WeaklyIncrementable O >
requires IndirectlyCopyable<I, O>
ranges::tagged_pair<tag::in(I), tag::out(O)>
copy( I first, S last, O result );
(2) (ranges TS)
template< InputRange R, WeaklyIncrementable O >
requires IndirectlyCopyable<ranges::iterator_t<R>, O>
ranges::tagged_pair<tag::in(ranges::safe_iterator_t<R>), tag::out(O)>
copy( R&& r, O result );
(3) (ranges TS)
template< InputIterator I, Sentinel<I> S, WeaklyIncrementable O,
class Proj = ranges::identity,
IndirectUnaryPredicate<projected<I, Proj>> Pred >
requires IndirectlyCopyable<I, O>
ranges::tagged_pair<tag::in(I), tag::out(O)>
copy_if( I first, S last, O result, Pred pred, Proj proj = Proj{} );
(4) (ranges TS)
template< InputRange R, WeaklyIncrementable O,
class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>,
Proj>> Pred >
requires IndirectlyCopyable<iterator_t<R>, O>
ranges::tagged_pair<tag::in(ranges::safe_iterator_t<R>),
tag::out(O)>
copy_if( R&& r, O result, Pred pred, Proj proj = Proj{} );
Okay so whatβs the vibe? It copies elements from the source range β either [first, last) or r β into the destination range beginning at result, starting from the first element in the source range and proceeding to the last one. Iconic behavior, honestly.
- Copies all elements in the range
[first, last). For each non-negative integern < (last - first), performs*(result + n) = *(first + n). The behavior is undefined ifresultis within the range[first, last). In this case,ranges::copy_backwardmay be used instead. π¨ - Same as (1), but uses
ras the source range, as if byranges::copy(ranges::begin(r), ranges::end(r), result);except thatresultmay not be copied. - Only copies the elements for which the predicate
predreturns true when applied to the elementβs value as projected by the projectionproj. The order of the elements that are copied is preserved. The behavior is undefined if the source and the destination ranges overlap. π«£ - Same as (3), but uses
ras the source range, as if byranges::copy_if(ranges::begin(r), ranges::end(r), result, pred, proj);except thatresult,predandprojmay not be copied.
Notwithstanding the declarations depicted above, the actual number and order of template parameters for algorithm declarations is unspecified. Thus, if explicit template arguments are used when calling an algorithm, the program is probably non-portable. π
π Parameters π
first,lastβ the range of elements to copyrβ the range of elements to copyresultβ the beginning of the destination rangepredβ predicate to apply to the projected elementsprojβ 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 typeIthat compares equal to the sentinellast. - The second member, with the tag
tag::out, is the past-the-end iterator of theresultrange.
π§ Complexity π§
- Exactly
ranges::distance(first, last)assignments. - Exactly
ranges::distance(r)assignments. - Exactly
ranges::distance(first, last)applications of the corresponding projection and predicate. - Exactly
ranges::distance(r)applications of the corresponding projection and predicate.
πΈ Possible implementations πΈ
First version
template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O>
requires IndirectlyCopyable<I, O>()
ranges::tagged_pair<tag::in(I), tag::out(O)>
copy(I first, S last, O result)
{
for (; first != last; ++first, (void)++result)
*result = *first;
return {first, result};
}
Second version
template<InputRange R, WeaklyIncrementable O>
requires IndirectlyCopyable<ranges::iterator_t<R>, O>()
ranges::tagged_pair<tag::in(ranges::safe_iterator_t<R>), tag::out(O)>
copy(R&& r, O result)
{
return ranges::copy(ranges::begin(r), ranges::end(r), result);
}
Third version
template<InputIterator I, Sentinel<I> S, WeaklyIncrementable O,
class Proj = ranges::identity,
IndirectUnaryPredicate<projected<I, Proj>> Pred>
requires IndirectlyCopyable<I, O>()
ranges::tagged_pair<tag::in(I), 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<InputRange R, WeaklyIncrementable O,
class Proj = ranges::identity,
IndirectUnaryPredicate<projected<ranges::iterator_t<R>,
Proj>> Pred>
requires IndirectlyCopyable<ranges::iterator_t<R>, O>()
ranges::tagged_pair<tag::in(ranges::safe_iterator_t<R>), tag::out(O)>
copy_if(R&& r, O result, Pred pred, Proj proj = Proj{})
{
return ranges::copy_if(ranges::begin(r), ranges::end(r), 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 β yes bestie, thatβs the exact original blurb, and hereβs the exact code:
// Run this code
#include <experimental/ranges/algorithm>
#include <experimental/ranges/iterator>
#include <iostream>
#include <numeric>
#include <vector>
int main()
{
// see https://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 π
copyβ copies a range of elements to a new locationcopy_ifβ(function template)(C++11)copy_backwardβ copies a range of elements in backwards order(function template)reverse_copyβ creates a copy of a range that is reversed(function template)copy_nβ copies a number of elements to a new location(function template)fillβ assigns a range of elements a certain value(function template)remove_copy,remove_copy_ifβ copies a range of elements omitting those that satisfy specific criteria(function template)
π source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::experi...es::copy_if(3) π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms