跳到主要内容

std::ranges::unique_copy() 算法

// (1)
constexpr unique_copy_result<I, O>
unique_copy( I first, S last, O result, C comp = {}, Proj proj = {} );

// (2)
constexpr unique_copy_result<ranges::borrowed_iterator_t<R>, O>
unique_copy( R&& r, O result, C comp = {}, Proj proj = {} );

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

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

对于所有重载,ProjC 模板参数具有以下默认类型 std::identityranges::equal_to

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

  • (1):

    indirectly_copyable<I, O>
    && (forward_iterator<I>
    || (input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>)
    || indirectly_copyable_storable<I, O>)
  • (2):

    indirectly_copyable<ranges::iterator_t<R>, O>
    && (forward_iterator<ranges::iterator_t<R>>
    || (input_iterator<O> && same_as<ranges::range_value_t<R>, iter_value_t<O>>)
    || indirectly_copyable_storable<ranges::iterator_t<R>, O>)

(为提高可读性,此处省略了 std:: 命名空间)

辅助类型定义如下

template< class I, class O >
using unique_copy_result = ranges::in_out_result<I, O>;
  • (1) 将元素从源范围 [first; last) 复制到从 result 开始的目标范围,使得没有连续相等的元素。

    只复制每组相等元素的第一个元素。

    如果 std::invoke(comp, std::invoke(proj, *(i - 1)), std::invoke(proj, *i)) == true,则两个连续元素 *(i - 1)*i 被认为是等效的,其中 i 是范围 [first + 1, last) 中的迭代器。

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

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

注意

范围 [first; last) 和 [result; result + ranges::distance(first, last)) 不得重叠。

参数

first
last

要处理的源范围。

r

要处理的源范围。

result

指向目标范围的迭代器。

comp

用于比较投影元素的二元谓词。

proj

要应用于元素的投影。

返回值

给定 Nranges::distance(first, last)

{
last,
result + N
}

复杂度

恰好 N - 1 次应用相应的谓词 comp,并且不超过两倍于任何投影 proj 的应用次数。

异常

(无)

可能的实现

unique_copy(1) 和 unique_copy(2)
struct unique_copy_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_equivalence_relation<std::projected<I,
Proj>> C = ranges::equal_to>
requires std::indirectly_copyable<I, O> && (std::forward_iterator<I> or
(std::input_iterator<O> && std::same_as<std::iter_value_t<I>,
std::iter_value_t<O>>) or std::indirectly_copyable_storable<I, O>)
constexpr ranges::unique_copy_result<I, O>
operator()(I first, S last, O result, C comp = {}, Proj proj = {}) const
{
if (!(first == last))
{
std::iter_value_t<I> value = *first;
*result = value;
++result;
while (!(++first == last))
{
auto&& value2 = *first;
if (!std::invoke(comp, std::invoke(proj, value2),
std::invoke(proj, value)))
{
value = std::forward<decltype(value2)>(value2);
*result = value;
++result;
}
}
}

return {std::move(first), std::move(result)};
}

template<ranges::input_range R, std::weakly_incrementable O, class Proj = std::identity,
std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>,
Proj>> C = ranges::equal_to>
requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
(std::forward_iterator<ranges::iterator_t<R>> or
(std::input_iterator<O> && std::same_as<ranges::range_value_t<R>,
std::iter_value_t<O>>) ||
std::indirectly_copyable_storable<ranges::iterator_t<R>, O>)
constexpr ranges::unique_copy_result<ranges::borrowed_iterator_t<R>, O>
operator()(R&& r, O result, C comp = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
std::move(comp), std::move(proj));
}
};

inline constexpr unique_copy_fn unique_copy {};

示例

Main.cpp
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <list>
#include <string>
#include <type_traits>

void print(const auto& rem, const auto& v)
{
using V = std::remove_cvref_t<decltype(v)>;
constexpr bool sep {std::is_same_v<typename V::value_type, int>};
std::cout << rem << std::showpos;
for (const auto& e : v)
std::cout << e << (sep ? " " : "");
std::cout << '\n';
}

int main()
{
std::string s1 {"The string with many spaces!"};
print("s1: ", s1);

std::string s2;
std::ranges::unique_copy(
s1.begin(), s1.end(), std::back_inserter(s2),
[](char c1, char c2) { return c1 == ' ' && c2 == ' '; }
);
print("s2: ", s2);

const auto v1 = {-1, +1, +2, -2, -3, +3, -3};
print("v1: ", v1);
std::list<int> v2;
std::ranges::unique_copy(
v1, std::back_inserter(v2),
{}, // default comparator std::ranges::equal_to
[](int x) { return std::abs(x); } // projection
);
print("v2: ", v2);
}
输出
s1: The      string    with many       spaces!
s2: The string with many spaces!
v1: -1 +1 +2 -2 -3 +3 -3
v2: -1 +2 -3
本文源自此 CppReference 页面。它可能为了改进或编辑偏好而有所改动。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::ranges::unique_copy() 算法

// (1)
constexpr unique_copy_result<I, O>
unique_copy( I first, S last, O result, C comp = {}, Proj proj = {} );

// (2)
constexpr unique_copy_result<ranges::borrowed_iterator_t<R>, O>
unique_copy( R&& r, O result, C comp = {}, Proj proj = {} );

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

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

对于所有重载,ProjC 模板参数具有以下默认类型 std::identityranges::equal_to

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

  • (1):

    indirectly_copyable<I, O>
    && (forward_iterator<I>
    || (input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>)
    || indirectly_copyable_storable<I, O>)
  • (2):

    indirectly_copyable<ranges::iterator_t<R>, O>
    && (forward_iterator<ranges::iterator_t<R>>
    || (input_iterator<O> && same_as<ranges::range_value_t<R>, iter_value_t<O>>)
    || indirectly_copyable_storable<ranges::iterator_t<R>, O>)

(为提高可读性,此处省略了 std:: 命名空间)

辅助类型定义如下

template< class I, class O >
using unique_copy_result = ranges::in_out_result<I, O>;
  • (1) 将元素从源范围 [first; last) 复制到从 result 开始的目标范围,使得没有连续相等的元素。

    只复制每组相等元素的第一个元素。

    如果 std::invoke(comp, std::invoke(proj, *(i - 1)), std::invoke(proj, *i)) == true,则两个连续元素 *(i - 1)*i 被认为是等效的,其中 i 是范围 [first + 1, last) 中的迭代器。

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

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

注意

范围 [first; last) 和 [result; result + ranges::distance(first, last)) 不得重叠。

参数

first
last

要处理的源范围。

r

要处理的源范围。

result

指向目标范围的迭代器。

comp

用于比较投影元素的二元谓词。

proj

要应用于元素的投影。

返回值

给定 Nranges::distance(first, last)

{
last,
result + N
}

复杂度

恰好 N - 1 次应用相应的谓词 comp,并且不超过两倍于任何投影 proj 的应用次数。

异常

(无)

可能的实现

unique_copy(1) 和 unique_copy(2)
struct unique_copy_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O,
class Proj = std::identity,
std::indirect_equivalence_relation<std::projected<I,
Proj>> C = ranges::equal_to>
requires std::indirectly_copyable<I, O> && (std::forward_iterator<I> or
(std::input_iterator<O> && std::same_as<std::iter_value_t<I>,
std::iter_value_t<O>>) or std::indirectly_copyable_storable<I, O>)
constexpr ranges::unique_copy_result<I, O>
operator()(I first, S last, O result, C comp = {}, Proj proj = {}) const
{
if (!(first == last))
{
std::iter_value_t<I> value = *first;
*result = value;
++result;
while (!(++first == last))
{
auto&& value2 = *first;
if (!std::invoke(comp, std::invoke(proj, value2),
std::invoke(proj, value)))
{
value = std::forward<decltype(value2)>(value2);
*result = value;
++result;
}
}
}

return {std::move(first), std::move(result)};
}

template<ranges::input_range R, std::weakly_incrementable O, class Proj = std::identity,
std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>,
Proj>> C = ranges::equal_to>
requires std::indirectly_copyable<ranges::iterator_t<R>, O> &&
(std::forward_iterator<ranges::iterator_t<R>> or
(std::input_iterator<O> && std::same_as<ranges::range_value_t<R>,
std::iter_value_t<O>>) ||
std::indirectly_copyable_storable<ranges::iterator_t<R>, O>)
constexpr ranges::unique_copy_result<ranges::borrowed_iterator_t<R>, O>
operator()(R&& r, O result, C comp = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::move(result),
std::move(comp), std::move(proj));
}
};

inline constexpr unique_copy_fn unique_copy {};

示例

Main.cpp
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <list>
#include <string>
#include <type_traits>

void print(const auto& rem, const auto& v)
{
using V = std::remove_cvref_t<decltype(v)>;
constexpr bool sep {std::is_same_v<typename V::value_type, int>};
std::cout << rem << std::showpos;
for (const auto& e : v)
std::cout << e << (sep ? " " : "");
std::cout << '\n';
}

int main()
{
std::string s1 {"The string with many spaces!"};
print("s1: ", s1);

std::string s2;
std::ranges::unique_copy(
s1.begin(), s1.end(), std::back_inserter(s2),
[](char c1, char c2) { return c1 == ' ' && c2 == ' '; }
);
print("s2: ", s2);

const auto v1 = {-1, +1, +2, -2, -3, +3, -3};
print("v1: ", v1);
std::list<int> v2;
std::ranges::unique_copy(
v1, std::back_inserter(v2),
{}, // default comparator std::ranges::equal_to
[](int x) { return std::abs(x); } // projection
);
print("v2: ", v2);
}
输出
s1: The      string    with many       spaces!
s2: The string with many spaces!
v1: -1 +1 +2 -2 -3 +3 -3
v2: -1 +2 -3
本文源自此 CppReference 页面。它可能为了改进或编辑偏好而有所改动。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。