std::ranges::remove_copy, std::ranges::remove_copy_if, std::ranges::remove_copy_result, std::ranges::remove_copy_if_result
来自cppreference.com
| 在标头 <algorithm> 定义
|
||
| 调用签名 |
||
| (1) | ||
| (C++20 起) (C++26 前) |
||
| |
(C++26 起) | |
| (2) | ||
| |
(C++20 起) (C++26 前) |
|
| |
(C++26 起) | |
| (3) | (C++20 起) | |
| |
(4) | (C++20 起) |
| 辅助类型 |
||
| |
(5) | (C++20 起) |
| |
(6) | (C++20 起) |
复制来自源范围 [first, last) 的元素到始于 result 的目标范围,忽略(在由 proj 投影后)满足特定判别标准的元素。若源范围与目标范围重叠则行为未定义。
1) 忽略所有等于
value 的元素。3) 忽略所有谓词
pred 对其返回 true 的元素。2,4) 同 (1,3),但以
r 为源范围,如同以 ranges::begin(r) 为 first 并以 ranges::end(r) 为 last。此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:
参数
| first, last | - | 要处理的源元素范围的迭代器-哨位对 |
| r | - | 要处理的源元素范围 |
| result | - | 目标范围的起始 |
| value | - | 不复制的元素的值 |
| comp | - | 比较投影后元素的二元谓词 |
| proj | - | 应用到元素的投影 |
返回值
{last, result + N},其中 N 是复制的元素数。
复杂度
准确应用 ranges::distance(first, last) 次对应的谓词 comp 与任何投影 proj。
注解
算法是稳定的,即保持被复制元素的相对顺序。
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 算法中的列表初始化 (1,2) |
可能的实现
| remove_copy (1,2) |
|---|
struct remove_copy_fn
{
template<std::input_iterator I, std::sentinel_for<I> S,
std::weakly_incrementable O, class Proj = std::identity,
class T = std::projected_value_t<I, Proj>>
requires std::indirectly_copyable<I, O> &&
std::indirect_binary_predicate<ranges::equal_to,
std::projected<I, Proj>, const T*>
constexpr ranges::remove_copy_result<I, O>
operator()(I first, S last, O result, const T& value, Proj proj = {}) const
{
for (; !(first == last); ++first)
if (value != std::invoke(proj, *first))
{
*result = *first;
++result;
}
return {std::move(first), std::move(result)};
}
template<ranges::input_range R,
std::weakly_incrementable O, class Proj = std::identity,
class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
std::indirect_binary_predicate<ranges::equal_to,
std::projected<ranges::iterator_t<R>, Proj>, const T*>
constexpr ranges::remove_copy_result<ranges::borrowed_iterator_t<R>, O>
operator()(R&& r, O result, const T& value, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::move(result), value,
std::move(proj));
}
};
inline constexpr remove_copy_fn remove_copy {};
|
| remove_copy_if (3,4) |
struct remove_copy_if_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
requires std::indirectly_copyable<I, O>
constexpr ranges::remove_copy_if_result<I, O>
operator()(I first, S last, O result, Pred pred, Proj proj = {}) const
{
for (; first != last; ++first)
if (false == std::invoke(pred, std::invoke(proj, *first)))
{
*result = *first;
++result;
}
return {std::move(first), std::move(result)};
}
template<ranges::input_range R, std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_unary_predicate<
std::projected<ranges::iterator_t<R>, Proj>> Pred>
requires std::indirectly_copyable<ranges::iterator_t<R>, O>
constexpr ranges::remove_copy_if_result<ranges::borrowed_iterator_t<R>, O>
operator()(R&& r, O result, Pred pred, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
std::move(pred), std::move(proj));
}
};
inline constexpr remove_copy_if_fn remove_copy_if {};
|
示例
运行此代码
#include <algorithm>
#include <array>
#include <complex>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string_view>
#include <vector>
void println(const auto rem, const auto& v)
{
std::cout << rem << ' ';
for (const auto& e : v)
std::cout << e << ' ';
std::cout << '\n';
}
int main()
{
// 从给定的字符串滤出散列记号。
const std::string_view str{"#Small #Buffer #Optimization"};
std::cout << "before: " << std::quoted(str) << '\n';
std::cout << "after: \"";
std::ranges::remove_copy(str.begin(), str.end(),
std::ostream_iterator<char>(std::cout), '#');
std::cout << "\"\n";
// 仅复制有正虚部的复数
using Ci = std::complex<int>;
constexpr std::array<Ci, 5> source
{
Ci{1, 0}, Ci{0, 1}, Ci{2, -1}, Ci{3, 2}, Ci{4, -3}
};
std::vector<std::complex<int>> target;
std::ranges::remove_copy_if
(
source,
std::back_inserter(target),
[](int imag) { return imag <= 0; },
[](Ci z) { return z.imag(); }
);
println("source:", source);
println("target:", target);
std::vector<std::complex<float>> nums{{2, 2}, {1, 3}, {4, 8}, {1, 3}};
std::vector<std::complex<double>> outs;
#ifdef __cpp_lib_algorithm_default_value_type
std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs),
{1, 3}); // T 被推导为 std::complex<float>
#else
std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs),
std::complex<float>{1, 3});
#endif
println("nums: ", nums);
println("outs: ", outs);
}
输出:
before: "#Small #Buffer #Optimization"
after: "Small Buffer Optimization"
source: (1,0) (0,1) (2,-1) (3,2) (4,-3)
target: (0,1) (3,2)
nums: (2,2) (1,3) (4,8) (1,3)
outs: (2,2) (4,8)
