跳到主要内容

std::ranges::remove_copy_if() 算法

// (1)
constexpr remove_copy_if_result<I, O>
remove_copy_if( I first, S last, O result, Pred pred, Proj proj = {} );

// (2)
constexpr remove_copy_if_result<ranges::borrowed_iterator_t<R>, O>
remove_copy_if( R&& r, O result, Pred pred, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • O - std::weakly_incrementable
  • R - std::input_range
  • Proj - (无)
  • Pred:
    • (1) - std::indirect_unary_predicate<std::projected<I, Proj>>
    • (2) - std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>

对于所有重载,Proj 模板参数的默认类型为 std::identity

此外,每个重载都有以下约束

  • (1) - std::indirectly_copyable<I, O>
  • (2) - std::indirectly_copyable<ranges::iterator_t<R>, O>

辅助类型定义如下:

template< class I, class O >
using remove_copy_if_result = ranges::in_out_result<I, O>;
  • (1) 忽略谓词 pred 返回 true 的所有元素。

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

移除通过移动赋值来完成,即移动范围内的元素,使不需要移除的元素出现在范围的开头。

重要

保留其余元素的相对顺序,且容器的物理大小不变。

警告

指向新逻辑末尾和范围物理末尾之间元素的迭代器仍然可以解引用但元素本身的值是未指定的(根据 MoveAssignable 后置条件)。 (自 C++11 起)

本页描述的函数类实体是niebloids

参数

first
last

要复制的元素范围。

r

要复制的元素范围。

result

目标范围的开头。

pred

如果元素应被忽略,则返回 true 的一元谓词。

proj

要应用于元素的投影。

返回值

{
last,
result + N
}

其中 N 是复制元素的数量。

复杂度

精确执行 ranges::distance(first, last) 次谓词 pred 和投影 proj 的应用。

异常

(无)

可能的实现

remove_copy_if(1)
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 {};

备注

该算法是稳定的,即保留复制元素的相对顺序。

示例

Main.cpp
#include <algorithm>
#include <array>
#include <complex>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string_view>
#include <vector>

void print(const auto rem, const auto& v)
{
std::cout << rem << ' ';
for (const auto& e : v)
std::cout << e << ' ';
std::cout << '\n';
}

int main()
{
// Filter out the hash symbol from the given string.
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";

// Copy only the complex numbers with positive imaginary part.
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(); }
);

print("source:", source);
print("target:", target);
}
输出
before: "#Small #Buffer #Optimization"
after: "Small Buffer Optimization"
source: (1,0) (0,1) (2,-1) (3,2) (4,-3)
target: (0,1) (3,2)
本文源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::ranges::remove_copy_if() 算法

// (1)
constexpr remove_copy_if_result<I, O>
remove_copy_if( I first, S last, O result, Pred pred, Proj proj = {} );

// (2)
constexpr remove_copy_if_result<ranges::borrowed_iterator_t<R>, O>
remove_copy_if( R&& r, O result, Pred pred, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • O - std::weakly_incrementable
  • R - std::input_range
  • Proj - (无)
  • Pred:
    • (1) - std::indirect_unary_predicate<std::projected<I, Proj>>
    • (2) - std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>

对于所有重载,Proj 模板参数的默认类型为 std::identity

此外,每个重载都有以下约束

  • (1) - std::indirectly_copyable<I, O>
  • (2) - std::indirectly_copyable<ranges::iterator_t<R>, O>

辅助类型定义如下:

template< class I, class O >
using remove_copy_if_result = ranges::in_out_result<I, O>;
  • (1) 忽略谓词 pred 返回 true 的所有元素。

  • (2)(1) 相同,但使用 r 作为源范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

移除通过移动赋值来完成,即移动范围内的元素,使不需要移除的元素出现在范围的开头。

重要

保留其余元素的相对顺序,且容器的物理大小不变。

警告

指向新逻辑末尾和范围物理末尾之间元素的迭代器仍然可以解引用但元素本身的值是未指定的(根据 MoveAssignable 后置条件)。 (自 C++11 起)

本页描述的函数类实体是niebloids

参数

first
last

要复制的元素范围。

r

要复制的元素范围。

result

目标范围的开头。

pred

如果元素应被忽略,则返回 true 的一元谓词。

proj

要应用于元素的投影。

返回值

{
last,
result + N
}

其中 N 是复制元素的数量。

复杂度

精确执行 ranges::distance(first, last) 次谓词 pred 和投影 proj 的应用。

异常

(无)

可能的实现

remove_copy_if(1)
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 {};

备注

该算法是稳定的,即保留复制元素的相对顺序。

示例

Main.cpp
#include <algorithm>
#include <array>
#include <complex>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string_view>
#include <vector>

void print(const auto rem, const auto& v)
{
std::cout << rem << ' ';
for (const auto& e : v)
std::cout << e << ' ';
std::cout << '\n';
}

int main()
{
// Filter out the hash symbol from the given string.
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";

// Copy only the complex numbers with positive imaginary part.
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(); }
);

print("source:", source);
print("target:", target);
}
输出
before: "#Small #Buffer #Optimization"
after: "Small Buffer Optimization"
source: (1,0) (0,1) (2,-1) (3,2) (4,-3)
target: (0,1) (3,2)
本文源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。