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


C++ std::unique_copy用法及代碼示例

std::unique用於刪除在range [first,last)中連續存在的任何元素的重複項。它對連續存在相同元素的範圍內存在的所有sub-groups執行此任務。

但是,如果我們不想改變原始範圍,而隻希望將std::unique的結果複製到另一個容器中,為此,我們在中定義了另一個函數,即std::unique_copy()。在這種情況下,僅複製範圍為[first,last)的每個連續的等效元素組中的第一個元素。

可以按以下兩種方式使用它:


  1. 使用==比較元素:
    用法:
    template 
      OutputIterator unique_copy (InputIterator first, InputIterator last,
                                  OutputIterator result);
    
    first: Forward iterator to the first element in the container.
    last: Forward iterator to the last element in the container.
    result: Output iterator to the initial position 
    of the container where the resulting range of values is stored. 
    The value being pointed should be compatible with the value being assigned.
    
    返回值:An iterator pointing to the end 
    of the copied range, which contains no consecutive duplicates.
    
    // C++ program to demonstrate  
    // the use of std::unique_copy 
    #include <iostream> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 
    int main() 
    { 
        vector<int> v = { 10, 10, 30, 30, 30, 100, 10, 
                          300, 300, 70, 70, 80 }; 
      
        // Declaring a vector to store the copied value 
        vector<int> v1(10); 
      
        vector<int>::iterator ip; 
      
        // Using std::unique_copy 
        ip = std::unique_copy(v.begin(), v.begin() + 12, v1.begin()); 
        // Now v1 contains {10 30 100 10 30 70 80 0 0 0} 
      
        // Resizing vector v1 
        v1.resize(std::distance(v1.begin(), ip)); 
      
        cout << "Before:"; 
        for (ip = v.begin(); ip != v.end(); ++ip)  
        { 
            cout << *ip << " "; 
        } 
      
        // Displaying the vector after applying std::unique_copy 
        cout << "\nAfter:"; 
        for (ip = v1.begin(); ip != v1.end(); ++ip)  
        { 
            cout << *ip << " "; 
        } 
      
        return 0; 
    }

    輸出:

    Before:10 10 30 30 30 100 10 300 300 70 70 80
    After:10 30 100 10 30 70 80 
    

    在此,在該向量中,具有連續重複元素的所有sub-groups已被簡化為一個元素,並已被複製到向量v1中。

  2. 通過使用預定義函數進行比較:
    用法:
    template 
      OutputIterator unique_copy (InputIterator first, InputIterator last,
                                  OutputIterator result, BinaryPredicate pred);
    
    Here, first, last and result are the same as previous case.
    
    Pred: Binary function that accepts two elements 
    in the range as argument, and returns a value convertible to bool. 
    The value returned indicates whether both arguments are considered equivalent
    (if true, they are equivalent and one of them is removed).
    The function shall not modify any of its arguments.
    This can either be a function pointer or a function object.
    
    返回值:It returns an iterator pointing to the 
    end of the copied range, which contains no consecutive duplicates.
    // C++ program to demonstrate the  
    // use of std::unique_copy 
    #include <iostream> 
    #include <algorithm> 
    #include <string> 
    using namespace std; 
      
    // Defining the BinaryFunction 
    bool Pred(char a, char b) 
    { 
        // Checking if 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 GFG", s1; 
      
        // Using std::unique_copy to remove the consecutive 
        // v in the word and copy it to s1 
        auto ip = std::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 GFG
    After:You arre visiting GFG
    

Where can it be used ?

  1. std::unique_copy可用於刪除所有重複元素(無論是否連續)並將結果存儲在另一個容器中,而不會影響先前的容器。
    // C++ program to demonstrate the use of std::unique_copy 
    #include <iostream> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 
    int main() 
    { 
        vector<int> v = { 1, 2, 3, 3, 3, 10, 1, 2, 3, 7, 7, 8 }; 
      
        vector<int>::iterator ip; 
      
        // Sorting the array to make duplicate elements 
        // consecutive 
        std::sort(v.begin(), v.end()); 
        // Now v becomes 1 1 2 2 3 3 3 3 7 7 8 10 
      
        // Declaring a container to store the unique elements 
        vector<int> v1(6); 
      
        // Using std::unique_copy 
        ip = std::unique_copy(v.begin(), v.begin() + 12, v1.begin()); 
        // Now v1 becomes {1 2 3 7 8 10} 
      
        // Displaying the vector v and v1 after applying std::unique 
        cout << "v = "; 
      
        for (ip = v.begin(); ip != v.end(); ++ip)  
        { 
            cout << *ip << " "; 
        } 
      
        cout << "\nv1 = "; 
        for (ip = v1.begin(); ip != v1.end(); ++ip)  
        { 
            cout << *ip << " "; 
        } 
      
        return 0; 
    }

    輸出:

    v = 1 1 2 2 3 3 3 3 7 7 8 10
    v1 = 1 2 3 7 8 10
    

    說明:在這裏,我們首先對向量進行排序以使重複的元素連續,然後應用std::unique_copy刪除重複的元素並將唯一元素存儲在另一個容器中。

  2. 我們還可以使用std::unique_copy查找容器是否僅包含唯一元素。這隻是上述將唯一元素存儲在另一個容器中的概念的擴展。此後,我們可以比較兩個容器以檢查它們是否相等。
    // C++ program to demonstrate the use of std::unique_copy 
    #include <iostream> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 
    int main() 
    { 
        vector<int> v = { 1, 20, 3, 10, 2, 7, 8 }; 
      
        vector<int>::iterator ip; 
      
        // Sorting the array to make duplicate elements 
        // consecutive 
        std::sort(v.begin(), v.end()); 
        // Now v becomes 1 2 3 7 8 10 20 
      
        // Declaring a container to store the unique elements 
        vector<int> v1(7); 
      
        // Using std::unique_copy 
        ip = std::unique_copy(v.begin(), v.end(), v1.begin()); 
        // Now v1 becomes {1 2 3 7 8 10 20} 
      
        if (v == v1)  
        { 
            cout << "v1 contains only unique elements"; 
        }  
        else 
        { 
            cout << "v1 contains duplicate elements"; 
        } 
        return 0; 
    }

    輸出:

    v1 contains only unique elements
    

    說明:首先,我們刪除重複的元素並將結果存儲在另一個容器中,然後將結果容器與先前的容器進行比較,如果兩者相同,則意味著先前的容器僅包含唯一元素,並且沒有重複的元素。



相關用法


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