名稱空間
變體
操作

std::unique

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
未初始化記憶體上的操作
 
定義於標頭檔案 <algorithm>
template< class ForwardIt >
ForwardIt unique( ForwardIt first, ForwardIt last );
(1) (C++20 起為 constexpr)
template< class ExecutionPolicy, class ForwardIt >

ForwardIt unique( ExecutionPolicy&& policy,

                  ForwardIt first, ForwardIt last );
(2) (C++17 起)
template< class ForwardIt, class BinaryPred >
ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPred p );
(3) (C++20 起為 constexpr)
template< class ExecutionPolicy,

          class ForwardIt, class BinaryPred >
ForwardIt unique( ExecutionPolicy&& policy,

                  ForwardIt first, ForwardIt last, BinaryPred p );
(4) (C++17 起)

從範圍 [firstlast) 中,移除每個連續的等價元素組中除第一個元素外的所有元素,並返回指向新範圍末尾的後尾迭代器。

1) 使用 operator== 比較元素。
如果 operator== 未建立等價關係,則行為未定義。
3) 使用給定二元謂詞 p 比較元素。
如果 p 未建立等價關係,則行為未定義。
2,4)(1,3),但按 policy 執行。
僅當滿足所有以下條件時,這些過載才參與過載決議

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>true

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>true

(C++20 起)

目錄

[edit] 說明

移除是透過移動範圍中的元素來完成的,使得不被移除的元素出現在範圍的開頭。

  • 移動是透過 複製賦值(直到 C++11)移動賦值(自 C++11 起)
  • 移除操作是穩定的:未被移除的元素的相對順序保持不變。
  • 移除操作不會縮短 [firstlast) 的底層序列。給定 result 作為返回的迭代器
  • [resultlast) 的每個元素都處於有效但未指定的狀態,因為移動賦值可以透過從原本在該範圍內的元素中移動來消除元素。
(C++11 起)

[edit] 引數

first, last - 定義要處理的元素範圍的迭代器對
policy - 要使用的 執行策略
p - 二元謂詞,如果元素應被視為相等,則返回 true。

謂詞函式的簽名應等效於以下內容:

 bool pred(const Type1 &a, const Type2 &b);

雖然簽名不需要包含 const &,但函式不得修改傳遞給它的物件,並且必須能夠接受 Type1Type2 的所有(可能是 const)型別值,無論其值類別如何(因此,不允許使用 Type1 &,除非對於 Type1 移動等同於複製,否則也不允許使用 Type1(自 C++11 起))。
型別 Type1Type2 必須使得型別為 ForwardIt 的物件可以被解引用,然後隱式轉換為兩者。​

型別要求
-
ForwardIt 必須滿足 LegacyForwardIterator 的要求。
-
解引用 ForwardIt 的型別必須滿足 可移動賦值 (MoveAssignable) 的要求。

[edit] 返回值

指向範圍新末尾的 ForwardIt

[edit] 複雜度

給定 N 作為 std::distance(first, last)

1,2) 使用 operator== 進行恰好 max(0,N-1) 次比較。
3,4) 恰好 max(0,N-1) 次應用謂詞 p

[edit] 異常

帶有模板引數 ExecutionPolicy 的過載按如下方式報告錯誤

  • 如果作為演算法一部分呼叫的函式執行期間丟擲異常,並且 ExecutionPolicy標準策略之一,則呼叫 std::terminate。對於任何其他 ExecutionPolicy,行為是實現定義的。
  • 如果演算法未能分配記憶體,則丟擲 std::bad_alloc

[edit] 可能實現

另請參閱 libstdc++libc++MSVC STL 中的實現。

unique (1)
template<class ForwardIt>
ForwardIt unique(ForwardIt first, ForwardIt last)
{
    if (first == last)
        return last;
 
    ForwardIt result = first;
    while (++first != last)
        if (!(*result == *first) && ++result != first)
            *result = std::move(*first);
 
    return ++result;
}
unique (3)
template<class ForwardIt, class BinaryPredicate>
ForwardIt unique(ForwardIt first, ForwardIt last, BinaryPredicate p)
{
    if (first == last)
        return last;
 
    ForwardIt result = first;
    while (++first != last)
        if (!p(*result, *first) && ++result != first)
            *result = std::move(*first);
 
    return ++result;
}

[edit] 注意

unique 的呼叫通常會後接對容器的 erase 成員函式的呼叫,以實際從容器中移除元素。

[edit] 示例

#include <algorithm>
#include <iostream>
#include <vector>
 
int main()
{
    // a vector containing several duplicate elements
    std::vector<int> v{1, 2, 1, 1, 3, 3, 3, 4, 5, 4};
    auto print = [&](int id)
    {
        std::cout << "@" << id << ": ";
        for (int i : v)
            std::cout << i << ' ';
        std::cout << '\n';
    };
    print(1);
 
    // remove consecutive (adjacent) duplicates
    auto last = std::unique(v.begin(), v.end());
    // v now holds {1 2 1 3 4 5 4 x x x}, where 'x' is indeterminate
    v.erase(last, v.end());
    print(2);
 
    // sort followed by unique, to remove all duplicates
    std::sort(v.begin(), v.end()); // {1 1 2 3 4 4 5}
    print(3);
 
    last = std::unique(v.begin(), v.end());
    // v now holds {1 2 3 4 5 x x}, where 'x' is indeterminate
    v.erase(last, v.end());
    print(4);
}

輸出

@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

[edit] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 202 C++98 如果元素使用非等價關係進行比較,則行為不明確。
如果元素使用非等價關係進行比較,行為不明確
在這種情況下,行為是
未定義的

[edit] 參閱

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