當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Algorithm unique_copy()用法及代碼示例

C++ 算法 unique_copy() 函數用於複製一個序列,例如每個重複的連續元素成為唯一元素。

  • 它不會改變原始範圍並將結果複製到另一個容器中。
  • 第一個版本使用 operator== 來比較元素,第二個版本使用給定的二元謂詞 pred。

用法

equality (1)	template <class InputIterator, class OutputIterator>
                            OutputIterator unique_copy (InputIterator first, InputIterator last,
                                                 OutputIterator result);

predicate (2)    template <class InputIterator, class OutputIterator, class BinaryPredicate>
                        OutputIterator unique_copy (InputIterator first, InputIterator last,
                                                 OutputIterator result, BinaryPredicate pred);

參數

first:一個向前迭代器,指向要複製的範圍內第一個元素的位置。

last:一個向前迭代器,指向要複製的範圍中最後一個元素之後的位置。

pred: 一個用戶定義的謂詞函數對象,它定義了在一個範圍內的兩個元素被視為等價時要滿足的條件。二元謂詞返回兩個參數,滿足時返回真,不滿足時返回假。

result: 一個輸出迭代器,指向複製範圍中第一個元素的位置,該元素正在接收刪除連續重複項的副本。

返回值

指向不包含連續重複項的複製範圍 [first, last) 的新結尾的迭代器。

複雜度

複雜度在[first, last)範圍內是線性的:比較每對連續的元素,並對其中的一些進行賦值操作。

數據競爭

訪問範圍 [first, last) 中的對象,並修改 result 和返回值之間範圍內的對象。

異常安全

如果 pred、元素比較、元素賦值或迭代器上的操作中的任何一個拋出異常,則此函數將拋出異常。

請注意,無效參數會導致未定義的行為。

例子1

讓我們看一個簡單的例子來演示 unique_copy() 的使用,其中元素將通過 operator== 進行比較:

#include <iostream> 
#include <vector> 
#include <algorithm> 
using namespace std; 
int main() 
{ 
    vector<int> v = { 100, 100, 300, 300, 300, 500, 100, 
                      300, 300, 600, 600, 700 }; 
  
    // vector to store the copied value 
    vector<int> v1(10); 
  
    vector<int>::iterator ip; 
  
    // Using unique_copy 
    ip = unique_copy(v.begin(), v.begin() + 12, v1.begin()); 
  
    // Resizing vector v1 
    v1.resize(distance(v1.begin(), ip)); 
  
    cout << "Before:"; 
    for (ip = v.begin(); ip != v.end(); ++ip)  
    { 
        cout << *ip << " "; 
    } 
  
    // Displaying vector after applying unique_copy 
    cout << "\n\nAfter: "; 
    for (ip = v1.begin(); ip != v1.end(); ++ip)  
    { 
        cout << *ip << " "; 
    } 
  
    return 0; 
}

輸出:

Before:100 100 300 300 300 500 100 300 300 600 600 700 

After: 100 300 500 100 300 600 700  

在上麵的例子中,向量 v 的所有連續重複元素的 sub-group 都被減少到隻有一個元素並複製到新的向量 v1。

例子2

讓我們看另一個簡單的例子來說明 unique_copy() 的使用,其中元素將通過預定義的函數進行比較:

#include <iostream> 
#include <algorithm> 
#include <string> 
using namespace std; 
  
// declaring a BinaryFunction 
bool Pred(char a, char b) 
{ 
    // It checks if the both the arguments are same and equal 
    // to 'v' then only they are considered same and duplicates are removed 
    if (a == b && a == 'v')  
    { 
        return 1; 
    }  
    else 
    { 
        return 0; 
    } 
} 
int main() 
{ 
    // Declaring a string 
    string s = "You arre vvvisiting vvvogie bbogie", s1; 
  
    // Using std::unique_copy to remove the consecutive 
    // v in the word and copy it to s1 
    auto ip = unique_copy(s.begin(), s.end(), back_inserter(s1), Pred); 
  
    cout << "Before:" << s; 
  
    // Displaying the corrected string 
    cout << "\nAfter:" << s1; 
    return 0; 
}

輸出:

Before:You arre vvvisiting vvvogie bbogie
After:You arre visiting vogie bbogie

例子3

讓我們看另一個簡單的例子來檢查容器是否包含重複元素:

#include <iostream> 
#include <vector> 
#include <algorithm> 

using namespace std; 

int main() 
{ 
    vector<int> v = { 1, 4, 3, 5, 2, 7, 6 }; 
  
    vector<int>::iterator ip; 
  
    // Sorting the array to make duplicate elements 
    // consecutive 
    sort(v.begin(), v.end()); 
    // Now v becomes 1 2 3 4 5 6 7 
  
    // Declaring a container to store the unique elements 
    vector<int> v1(7); 
  
    // Using unique_copy 
    ip = unique_copy(v.begin(), v.end(), v1.begin()); 
    // Now v1 becomes {1 2 3 4 5 6 7 } 
  
    if (v == v1)  
    { 
        cout << "v1 contains only unique elements"; 
    }  
    else 
    { 
        cout << "v1 contains duplicate elements"; 
    } 
    return 0; 
}

輸出:

v1 contains only unique elements

在上麵的示例中,首先我們從向量 v 中刪除重複元素並將結果元素存儲到另一個向量 v1 中,然後將 v1 與 v 進行比較,如果兩者相同。 (意味著之前的向量 v 隻包含唯一元素並且沒有重複元素)。

示例 4

讓我們看另一個從給定語句中刪除所有空格的簡單示例:

#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;
 
int main()
{
    string s1 = "The string with many spaces!";
    cout << "before: " << s1 << '\n';
 
    string s2;
    unique_copy(s1.begin(), s1.end(), std::back_inserter(s2),
                     [](char c1, char c2){ return c1 == ' ' && c2 == ' '; });
 
    cout << "after:  " << s2 << '\n';
}

輸出:

before: The      string    with many       spaces!
after:   The string with many spaces!





相關用法


注:本文由純淨天空篩選整理自 C++ Algorithm unique_copy()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。