当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。