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


C++ std::unique用法及代码示例


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

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

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

  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.
    
    返回值: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 <iostream> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 
    int main() 
    { 
        vector<int> v = { 1, 1, 3, 3, 3, 10, 1, 3, 3, 7, 7, 8 }, i; 
      
        vector<int>::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
    

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

  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.
    
    返回值: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 <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 '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代表arre,v代表访问)。

哪里可以使用str::unique?

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

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

    // C++ program to demonstrate the use of std::unique 
    #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 
        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 <iostream> 
    #include <iterator> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 
    int main() 
    { 
        vector<int> v = { 1, 1, 3, 3, 3, 10, 1, 3, 3, 7, 7, 8 }; 
      
        vector<int>::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的帮助下从开始到新结束之间的元素数量,应为我们提供总数。容器中独特元素的集合。



相关用法


注:本文由纯净天空筛选整理自 std::unique in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。