C++ 算法 unique() 函數用於轉換序列,使每個重複的連續元素成為唯一元素。
第一個版本使用 operator== 來比較元素,第二個版本使用給定的二元謂詞 pred。
用法
equality (1) template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last);
predicate (2) template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
參數
first:一個前向迭代器,指向要掃描的範圍中第一個元素的位置以進行重複刪除。
last:一個前向迭代器,指向要掃描的範圍中最後一個元素之後的位置以進行重複刪除。
pred: 一個用戶定義的謂詞函數對象,它定義了在一個範圍內的兩個元素被視為等價時要滿足的條件。二元謂詞返回兩個參數,滿足時返回真,不滿足時返回假。 0
返回值
指向不包含連續重複項的範圍 [first, last) 的新結尾的前向迭代器。
複雜度
複雜度在[first, last)範圍內是線性的:比較每對連續的元素,並對其中的一些進行賦值操作。
數據競爭
範圍 [first, last) 中的對象被訪問並可能被修改。
異常安全
如果 pred、元素比較、元素賦值或迭代器上的操作中的任何一個拋出異常,則此函數將拋出異常。
注意:無效的參數會導致未定義的行為。
例子1
讓我們看一個簡單的例子來演示 unique() 的使用:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
int main()
{
// remove duplicate elements
vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7};
sort(v.begin(), v.end());
auto last = unique(v.begin(), v.end());
// v now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate
v.erase(last, v.end());
for (int i:v)
cout << i << " ";
cout << "\n";
return 0;
}
輸出:
1 2 3 4 5 6 7
例子2
讓我們看另一個簡單的例子:
#include <iostream> // std::cout
#include <algorithm> // std::unique, std::distance
#include <vector> // std::vector
using namespace std;
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10
vector<int> myvector (myints,myints+9);
// using default comparison:
vector<int>::iterator it;
it = unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ?
// ^
myvector.resize( distance(myvector.begin(),it) ); // 10 20 30 20 10
// using predicate comparison:
unique (myvector.begin(), myvector.end(), myfunction); // (no changes)
// print out content:
std::cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
輸出:
myvector contains:10 20 30 20 10
例子3
讓我們看另一個簡單的例子:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void print(const char* tag, const vector<int>& v) {
cout << tag << ":";
bool first = true;
for (int x:v) {
if (first) {
first = false;
}
else {
cout << ',';
}
cout << x;
}
cout << endl;
}
int main() {
{
vector<int> v = { 2,5,3,3,1,2,4,2,1,1,4,4,3,3,3 };
decltype(v)::iterator result = unique(v.begin(), v.end());
v.erase(result, v.end());
print("unsorted unique", v);
}
{
vector<int> v = { 2,5,3,3,1,2,4,2,1,1,4,4,3,3,3 };
sort(v.begin(), v.end());
decltype(v)::iterator result = unique(v.begin(), v.end());
v.erase(result, v.end());
print("sorted unique", v);
}
}
輸出:
unsorted unique:2,5,3,1,2,4,2,1,4,3 sorted unique:1,2,3,4,5
示例 4
讓我們看另一個簡單的例子:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
int main()
{
// remove duplicate elements (normal use)
vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7};
sort(v.begin(), v.end()); // 1 1 2 2 3 3 3 4 4 5 5 6 7
auto last = unique(v.begin(), v.end());
// v now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate
v.erase(last, v.end());
for (int i:v)
cout << i << " ";
cout << "\n";
// remove consecutive spaces
string s = "wanna go to space?";
auto end = unique(s.begin(), s.end(), [](char l, char r){
return isspace(l) && isspace(r) && l == r;
});
// s now holds "wanna go to space?xxxxxxxx", where 'x' is indeterminate
cout << string(s.begin(), end) << '\n';
}
輸出:
1 2 3 4 5 6 7 wanna go to space?
例 5
讓我們看另一個例子:
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <ostream>
using namespace std;
// Return whether modulus of elem1 is equal to modulus of elem2
bool mod_equal ( int elem1, int elem2 )
{
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 == elem2;
};
int main( )
{
vector <int> v1;
vector <int>::iterator v1_Iter1, v1_Iter2, v1_Iter3,
v1_NewEnd1, v1_NewEnd2, v1_NewEnd3;
int i;
for ( i = 0 ; i <= 3 ; i++ )
{
v1.push_back( 5 );
v1.push_back( -5 );
}
int ii;
for ( ii = 0 ; ii <= 3 ; ii++ )
{
v1.push_back( 4 );
}
v1.push_back( 7 );
cout << "Vector v1 is ( " ;
for ( v1_Iter1 = v1.begin( ) ; v1_Iter1 != v1.end( ) ; v1_Iter1++ )
cout << *v1_Iter1 << " ";
cout << ")." << endl;
// Remove consecutive duplicates
v1_NewEnd1 = unique ( v1.begin ( ) , v1.end ( ) );
cout << "Removing adjacent duplicates from vector v1 gives\n ( " ;
for ( v1_Iter1 = v1.begin( ) ; v1_Iter1 != v1_NewEnd1 ; v1_Iter1++ )
cout << *v1_Iter1 << " ";
cout << ")." << endl;
// Remove consecutive duplicates under the binary prediate mod_equals
v1_NewEnd2 = unique ( v1.begin ( ) , v1_NewEnd1 , mod_equal );
cout << "Removing adjacent duplicates from vector v1 under the\n "
<< " binary predicate mod_equal gives\n ( " ;
for ( v1_Iter2 = v1.begin( ) ; v1_Iter2 != v1_NewEnd2 ; v1_Iter2++ )
cout << *v1_Iter2 << " ";
cout << ")." << endl;
// Remove elements if preceded by an element that was greater
v1_NewEnd3 = unique ( v1.begin ( ) , v1_NewEnd2, greater<int>( ) );
cout << "Removing adjacent elements satisfying the binary\n "
<< " predicate mod_equal from vector v1 gives ( " ;
for ( v1_Iter3 = v1.begin( ) ; v1_Iter3 != v1_NewEnd3 ; v1_Iter3++ )
cout << *v1_Iter3 << " ";
cout << ")." << endl;
return 0;
}
輸出:
Vector v1 is ( 5 -5 5 -5 5 -5 5 -5 4 4 4 4 7 ). Removing adjacent duplicates from vector v1 gives ( 5 -5 5 -5 5 -5 5 -5 4 7 ). Removing adjacent duplicates from vector v1 under the binary predicate mod_equal gives ( 5 4 7 ). Removing adjacent elements satisfying the binary predicate mod_equal from vector v1 gives ( 5 7 ).
相關用法
- C++ Algorithm unique_copy()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm random_shuffle()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm replace()用法及代碼示例
- C++ Algorithm set_intersection()用法及代碼示例
- C++ Algorithm lower_bound()用法及代碼示例
- C++ Algorithm transform()用法及代碼示例
- C++ Algorithm set_difference()用法及代碼示例
- C++ Algorithm is_sorted()用法及代碼示例
- C++ Algorithm equal_range()用法及代碼示例
- C++ Algorithm minmax_element()用法及代碼示例
- C++ Algorithm stable_sort()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm unique()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。