📜  std :: unique在C++中

📅  最后修改于: 2021-05-30 03:31:07             🧑  作者: Mango

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

  • 它不会删除所有重复的元素,而是通过将序列中存在的下一个元素替换为与当前要替换的当前元素不重复的下一个元素来消除重复性。所有被替换的元素都处于未指定状态。
  • 此函数的另一个有趣的功能是,在删除元素后不会更改容器的大小,它仅返回一个指向容器新端的指针,并根据此指针来调整容器的大小或删除容器的大小。垃圾元素。

可以按以下两种方式使用它:

  1. 使用==比较元素
    句法:
    template 
    ForwardIterator unique (ForwardIterator first, ForwardIterator last);
    
    first: Forward iterator to the first element in the container.
    last: forward iterator to the last element in the container.
    
    Return Value: It returns an iterator to the element that follows 
    the last element not removed. The range between first and this iterator includes 
    all the elements in the sequence that were not
    duplicates and hence not removed.
    
    // C++ program to demonstrate the use of std::unique
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 1, 1, 3, 3, 3, 10, 1, 3, 3, 7, 7, 8 }, i;
      
        vector::iterator ip;
      
        // Using std::unique
        ip = std::unique(v.begin(), v.begin() + 12);
        // Now v becomes {1 3 10 1 3 7 8 * * * * *}
        // * means undefined
      
        // Resizing the vector so as to remove the undefined terms
        v.resize(std::distance(v.begin(), ip));
      
        // Displaying the vector after applying std::unique
        for (ip = v.begin(); ip != v.end(); ++ip) {
            cout << *ip << " ";
        }
      
        return 0;
    }
    

    输出:

    1 3 10 1 3 7 8
    

    在此,在该向量中,具有连续重复元素的所有子组已被减少为仅一个元素。请注意,以后是否也存在相同的元素并不重要,此函数仅处理连续出现的重复元素。

  2. 通过使用预定义函数进行比较:
    句法:
    template 
     ForwardIterator unique (ForwardIterator first, ForwardIterator last,
                             BinaryPredicate Pred);
    
    Here, first and last 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 to the element that 
    follows the last element not removed.
    The range between first and this iterator includes 
    all the elements in the sequence that were not
    duplicates and hence not removed.
    // C++ program to demonstrate the use of std::unique
    #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 'G' then only they are considered same
        // and duplicates are removed
        if (a == b && a == 'G') {
            return 1;
        } else {
            return 0;
        }
    }
    int main()
    {
        // Declaring a string
        string s = "You arre vvvisiting GGGGFGGGG";
      
        // Using std::unique to remove the consecutive
        // G in the word
        auto ip = std::unique(s.begin(), s.end(), Pred);
      
        // Displaying the corrected string
        cout << string(s.begin(), ip);
        return 0;
    }
    

    输出:

    You arre vvvisiting GFG
    

    在这里,我们以一种方式操纵二进制函数,即只有将两个G作为参数传递时,才将它们视为相同,并且如果连续出现任何其他字符,则它将保持不受影响,并且不会被保留。已删除(如r引起误会,v引起了访问)。

哪里可以使用str :: unique?

  1. 从容器中删除所有重复的元素:你们中的许多人都必须搜索std :: unique并认为它将从容器中删除所有重复的元素,现在您可能会有些失望,因为知道它只能删除容器中的所有重复元素。连续重复的元素。但是,尽管std :: unique不能按照其定义进行操作,但是通过应用一些逻辑,我们可以实现这一目标。我们需要做的就是在应用std :: unique之前对数组进行排序,以使所有相等的元素成为连续的,现在我们有了std :: unique来删除所有重复的连续元素。

    因此,std :: unique也可以用于从容器中删除所有重复的元素。

    // C++ program to demonstrate the use of std::unique
    #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
        std::sort(v.begin(), v.end());
        // Now v becomes 1 1 2 2 3 3 3 3 7 7 8 10
      
        // Using std::unique
        ip = std::unique(v.begin(), v.begin() + 12);
        // Now v becomes {1 2 3 7 8 10 * * * * * *}
        // * means undefined
      
        // Resizing the vector so as to remove the undefined terms
        v.resize(std::distance(v.begin(), ip));
      
        // Displaying the vector after applying std::unique
        for (ip = v.begin(); ip != v.end(); ++ip) {
            cout << *ip << " ";
        }
      
        return 0;
    }
    

    输出:

    1 2 3 7 8 10
    

    说明:首先,我们对数组进行排序,以使所有相等的重复元素变为连续的,然后对其应用std :: unique,从而删除重复项,并以此方式从容器中删除所有重复的元素,无论是连续的还是重复的不是。

  2. 计算唯一元素:如果我们要计算总数,也可以使用它。容器中的独特元素的集合。
    // C++ program to demonstrate the use of std::unique
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 1, 1, 3, 3, 3, 10, 1, 3, 3, 7, 7, 8 };
      
        vector::iterator ip;
      
        int count;
        sort(v.begin(), v.end());
      
        // Using std::unique and std::distance to count
        // unique elements in a container
        count = std::distance(v.begin(),
                              std::unique(v.begin(), v.begin() + 12));
      
        // Displaying the value of count
        cout << "Total no. of unique elements = " << count;
      
        return 0;
    }
    

    输出:

    Total no. of unique elements = 5
    

    说明:我们知道std :: unique在删除重复的元素之后将迭代器返回到应该是容器的新端的迭代器,因此只计算总数就没有了。在std :: distance的帮助下从开始到新结束之间的元素数量,应该给我们总的数量。容器中独特元素的集合。

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