📜  C++中的std :: unique_copy

📅  最后修改于: 2021-05-30 14:53:27             🧑  作者: Mango

std :: unique用于删除在range [first,last)中连续存在的任何元素的重复项。它对具有连续存在的相同元素的范围内的所有子组执行此任务。

但是,如果我们不想改变原始范围,而只想将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.
    
    Return Value: 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 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 10, 10, 30, 30, 30, 100, 10,
                          300, 300, 70, 70, 80 };
      
        // Declaring a vector to store the copied value
        vector v1(10);
      
        vector::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 
    

    在此,在该向量中,具有连续重复元素的所有子组均被简化为一个元素,并已被复制到向量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.
    
    Return Value: 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 
    #include 
    #include 
    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
    

在哪里可以使用?

  1. std :: unique_copy可用于删除所有重复的元素(无论是否连续)并将结果存储在另一个容器中,而不会影响先前的容器。
    // C++ program to demonstrate the use of std::unique_copy
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 1, 2, 3, 3, 3, 10, 1, 2, 3, 7, 7, 8 };
      
        vector::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 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 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 1, 20, 3, 10, 2, 7, 8 };
      
        vector::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 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
    

    说明:首先,我们删除了重复的元素,并将结果存储在另一个容器中,然后将结果容器与先前的容器进行比较,如果两者相同,则意味着先前的容器仅包含唯一元素,并且没有重复的元素。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”