std::unique
定義於標頭檔案 <algorithm> |
||
template< class ForwardIt > ForwardIt unique( ForwardIt first, ForwardIt last ); |
(1) | (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt unique( ExecutionPolicy&& policy, |
(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 > |
(4) | (C++17 起) |
從範圍 [
first,
last)
中,移除每個連續的等價元素組中除第一個元素外的所有元素,並返回指向新範圍末尾的後尾迭代器。
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 起)。
- 移除操作是穩定的:未被移除的元素的相對順序保持不變。
- 移除操作不會縮短
[
first,
last)
的底層序列。給定 result 作為返回的迭代器
[
result,
last)
中的所有迭代器仍然可解引用。
|
(C++11 起) |
[edit] 引數
first, last | - | 定義要處理的元素範圍的迭代器對 |
policy | - | 要使用的 執行策略 |
p | - | 二元謂詞,如果元素應被視為相等,則返回 true。 謂詞函式的簽名應等效於以下內容: bool pred(const Type1 &a, const Type2 &b); 雖然簽名不需要包含 const &,但函式不得修改傳遞給它的物件,並且必須能夠接受 |
型別要求 | ||
-ForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-解引用 ForwardIt 的型別必須滿足 可移動賦值 (MoveAssignable) 的要求。 |
[edit] 返回值
指向範圍新末尾的 ForwardIt
。
[edit] 複雜度
給定 N 作為 std::distance(first, last)
[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> 的公共成員函式) | |
(C++20) |
移除一個範圍中的連續重複元素 (演算法函式物件) |