名稱空間
變體
操作

std::ranges::unique

來自 cppreference.com
< cpp‎ | 演算法‎ | 範圍
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
受約束演算法
此選單中的所有名稱均屬於名稱空間 std::ranges
非修改序列操作
修改序列操作
劃分操作
排序操作
二分查詢操作(在已排序的範圍內)
       
       
集合操作(於已排序範圍上)
堆操作
最小/最大值操作
       
       
排列操作
摺疊操作
數值操作
(C++23)            
對未初始化儲存的操作
返回型別
 
定義於標頭檔案 <algorithm>
呼叫簽名 (Call signature)
template< std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,

          std::indirect_equivalence_relation<std::projected<I, Proj>>
              C = ranges::equal_to >
constexpr ranges::subrange<I>

    unique( I first, S last, C comp = {}, Proj proj = {} );
(1) (C++20 起)
template< ranges::forward_range R, class Proj = std::identity,

          std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>, Proj>>
              C = ranges::equal_to >
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_subrange_t<R>

    unique( R&& r, C comp = {}, Proj proj = {} );
(2) (C++20 起)
1) 從範圍 [firstlast) 中刪除每個連續等價組中的所有元素,只保留第一個,並返回子範圍 [retlast),其中 ret 是新範圍末尾的後尾迭代器。
如果 std::invoke(comp, std::invoke(proj, *(i - 1)), std::invoke(proj, *i)) == true,則認為兩個連續元素 *(i - 1)*i 等價,其中 i 是範圍 [first + 1last) 中的迭代器。
2)(1),但使用 r 作為範圍,如同使用 ranges::begin(r) 作為 firstranges::end(r) 作為 last

本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即

目錄

[編輯] 引數

first, last - 定義要處理元素的範圍的迭代器-哨兵對
r - 要處理的元素範圍
comp - 用於比較投影元素的二元謂詞
proj - 應用於元素的投影

[編輯] 返回值

返回 {ret, last},其中 ret 是新範圍末尾的後尾迭代器。

[編輯] 複雜度

對於非空範圍,精確執行相應謂詞 comp ranges::distance(first, last) - 1 次,任何投影 proj 的執行次數不超過此的兩倍。

[編輯] 註解

透過移動賦值的方式,將範圍中的元素進行移位,使不需要移除的元素出現在範圍的開頭。保留剩餘元素的相對順序,容器的*物理*大小不變。範圍 [retlast) 中的迭代器(如果存在)仍然可以解引用,但元素本身具有未指定的值(根據MoveAssignable後置條件)。

ranges::unique 的呼叫有時會接著呼叫容器的 erase 成員函式,該函式會擦除未指定的值並將容器的*物理*大小減小以匹配其新的*邏輯*大小。這兩個呼叫共同構成了Erase–remove idiom

[編輯] 可能的實現

struct unique_fn
{
    template<std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_equivalence_relation<std::projected<I, Proj>>
                 C = ranges::equal_to>
    constexpr ranges::subrange<I>
        operator()(I first, S last, C comp = {}, Proj proj = {}) const
    {
        first = ranges::adjacent_find(first, last, comp, proj);
        if (first == last)
            return {first, first};
        auto i {first};
        ++first;
        while (++first != last)
            if (!std::invoke(comp, std::invoke(proj, *i), std::invoke(proj, *first)))
                *++i = ranges::iter_move(first);
        return {++i, first};
    }
 
    template<ranges::forward_range R, class Proj = std::identity,
             std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>, Proj>>
                 C = ranges::equal_to>
    requires std::permutable<ranges::iterator_t<R>>
    constexpr ranges::borrowed_subrange_t<R>
        operator()(R&& r, C comp = {}, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r),
                       std::move(comp), std::move(proj));
    }
};
 
inline constexpr unique_fn unique {};

[編輯] 示例

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <vector>
 
struct id {
    int i;
    explicit id(int i) : i {i} {}
};
 
void print(id i, const auto& v)
{
    std::cout << i.i << ") ";
    std::ranges::for_each(v, [](auto const& e) { std::cout << e << ' '; });
    std::cout << '\n';
}
 
int main()
{
    // a vector containing several duplicated elements
    std::vector<int> v {1, 2, 1, 1, 3, 3, 3, 4, 5, 4};
 
    print(id {1}, v);
 
    // remove consecutive (adjacent) duplicates
    const auto ret = std::ranges::unique(v);
    // v now holds {1 2 1 3 4 5 4 x x x}, where 'x' is indeterminate
    v.erase(ret.begin(), ret.end());
    print(id {2}, v);
 
    // sort followed by unique, to remove all duplicates
    std::ranges::sort(v); // {1 1 2 3 4 4 5}
    print(id {3}, v);
 
    const auto [first, last] = std::ranges::unique(v.begin(), v.end());
    // v now holds {1 2 3 4 5 x x}, where 'x' is indeterminate
    v.erase(first, last);
    print(id {4}, v);
 
    // unique with custom comparison and projection
    std::vector<std::complex<int>> vc { {1, 1}, {-1, 2}, {-2, 3}, {2, 4}, {-3, 5} };
    print(id {5}, vc);
 
    const auto ret2 = std::ranges::unique(vc,
        // consider two complex nums equal if their real parts are equal by module:
        [](int x, int y) { return std::abs(x) == std::abs(y); }, // comp
        [](std::complex<int> z) { return z.real(); }             // proj
    );
    vc.erase(ret2.begin(), ret2.end());
    print(id {6}, vc);
}

輸出

1) 1 2 1 1 3 3 3 4 5 4
2) 1 2 1 3 4 5 4
3) 1 1 2 3 4 4 5
4) 1 2 3 4 5
5) (1,1) (-1,2) (-2,3) (2,4) (-3,5)
6) (1,1) (-2,3) (-3,5)

[編輯] 參閱

建立一個不含連續重複元素的某個元素範圍的副本
(演算法函式物件)[編輯]
尋找第一對相等的(或滿足給定謂詞的)相鄰項
(演算法函式物件)[編輯]
移除滿足特定標準的元素
(演算法函式物件)[編輯]
移除一個範圍中的連續重複元素
(函式模板) [編輯]
移除連續的重複元素
(std::list<T,Allocator> 的公共成員函式) [編輯]
移除連續的重複元素
(std::forward_list<T,Allocator> 的公共成員函式) [編輯]