Namespaces
Variants

std::ranges::unique_copy, std::ranges::unique_copy_result

来自 zh.cppreference.net
 
 
算法库
受约束算法和范围上的算法 (C++20)
受约束算法,例如 ranges::copy、ranges::sort、...
排序和相关操作
划分操作
(C++11)    

排序操作
二分搜索操作
(在划分范围上)
集合操作(在排序范围上)
合并操作(在排序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
受约束算法
此菜单中的所有名称属于命名空间 std::ranges
不修改序列的操作
修改序列的操作
划分操作
排序操作
二分搜索操作(在排序范围上)
       
       
集合操作(在排序范围上)
堆操作
最小/最大操作
       
       
排列操作
折叠操作
数值操作
(C++23)            
未初始化存储上的操作
返回类型
 
定义于头文件 <algorithm>
调用签名
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> ||
             (std::input_iterator<O> && std::same_as<std::iter_value_t<I>,
                 std::iter_value_t<O>>) || std::indirectly_copyable_storable<I, O>)
constexpr unique_copy_result<I, O>
    unique_copy( I first, S last, O result, C comp = {}, Proj proj = {} );
(1) (自 C++20 起)
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>> ||
             (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 unique_copy_result<ranges::borrowed_iterator_t<R>, O>
    unique_copy( R&& r, O result, C comp = {}, Proj proj = {} );
(2) (自 C++20 起)
辅助类型
template< class I, class O >
using unique_copy_result = ranges::in_out_result<I, O>;
(3) (自 C++20 起)
1) 将源范围 [first, last) 中的元素复制到始于 result 的目标范围,使得没有连续的相等元素。只复制每组相等元素中的第一个元素。
范围 [first, last) 和 [result, result + N) 不得重叠。 N = ranges::distance(first, last)。
两个连续元素 *(i - 1) 和 *i 被认为是等价的,如果 std::invoke(comp, std::invoke(proj, *(i - 1)), std::invoke(proj, *i)) == true,其中 i 是范围 [first + 1, last) 中的迭代器。
2) 与 (1) 相同,但使用 r 作为范围,如同使用 ranges::begin(r) 作为 first,以及 ranges::end(r) 作为 last。

本页描述的仿函数实体是算法函数对象(非正式地称为niebloids),即:

参数

first, last - 定义待处理元素源 范围 的迭代器-哨位对
r - 元素的源范围
result - 元素的目标范围
comp - 用于比较投影元素的二元谓词
proj - 应用于元素的投影

返回值

{ last, result + N }

复杂度

恰好需要 N - 1 次对应谓词 comp 的应用,且任何投影 proj 的应用次数不超过其两倍。

可能的实现

另请参阅 libstdc++ 、 MSVC STL (以及第三方库: cmcstl2 、 NanoRange 和 range-v3 )中的实现。

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> ||
                 (std::input_iterator<O> && std::same_as<std::iter_value_t<I>,
                     std::iter_value_t<O>>) || 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>> ||
                 (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 {};

示例

#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),
        {}, // 默认比较器 std::ranges::equal_to
        [](int x) { return std::abs(x); } // 投影函数
    );
    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

参见

移除范围中连续的重复元素
(算法函数对象)
将元素范围复制到新位置
(算法函数对象)
查找首个相等的相邻元素(或满足给定谓词的相邻元素)
(算法函数对象)
创建不包含连续重复元素的区间副本
(函数模板)