How to use UNIQUE functions (STL library function)

tags: stl

UNIQUE function
UNIQUE () function is a default function, the function of Unique's function unique in STL is to remove adjacent repeating elements (only one), and an easy ignore feature is it It doesn't really delete the duplicate element. He is a function in C ++, so the header file should be added #include, the specific usage is as follows:

int num[10];

Unique (NUM, NUN + N) is returned by Num to the tail address of the non-repetitive element, which is said to be deleted than the repeated element, in fact, the function moves repeated elements to the back, Then it is still saved in the original number group and then returns the address of the last element, because the unique removes the adjacent repeating elements, so it is necessary to discharge the next order before use.

Below is a small example of using the UNIQUE function to retrieve, I believe that if you understand the following example, you will use the UNQIUE function to go back.

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int num[10]={1,2,3,8,7,5,3,1,2,4};
int main()
{
    sort(num,num+10);
    cout<<"Elements before going to the top:"<<endl;
    for(int i=0;i<10;i++)
    {
        cout<<num[i]<<" ";
    }
    cout<<endl;
    sort(num,num+10);
    int len=unique(num,num+10)-num;// Number of not repeated elements after heavy weight
    cout<<"The number of elements after returning is:"<<len<<endl;
    cout<<"The elements of the backward are:";
    for(int i=0;i<len;i++)
    {
        cout<<num[i]<<" ";
    }
    cout<<endl;
    return 0;
}
/*
 Before you get the element:
1 1 2 2 3 3 4 5 7 8
 The number of elements after retransmission is: 7
 The elements that are retained are: 1 2 3 4 5 7 8
*/

Of course, UNIQUE can also be re-emphasized.

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int main()
{
    string str("Helloword!_Helloword!");
    cout<<str<<endl;
    sort( str.begin(),str.end());// Sort
    cout<<str<<endl;
    str.erase(unique(str.begin(),str.end()),str.end());// unique () Move the duplicate element to the end and returns the last element of the last element
    cout<<str<<endl;
    return 0;
}

Intelligent Recommendation

c++ STL unique function

  This function keeps only one adjacent repeated element, overwrites the previous repeated element with the following element, and returns the tail address....

STL: unique () function

unique() Unique () is to remove the repetition. He is eliminated between the characters. If there is no repetition between the characters in which the characters in it have, the UNIQUE function is not...

STL UNIQUE () function learning

In STL, the unique () function is a default function. The function of Unique's function unique in the STL is removed.Adjacent(Description is a repeating element (only one) that has been sorted by the ...

STL - UNIQUE Removal Function

Learn this function in a piece of code from Peking University. There is no strict check in time, but it is very simple to write. Give one The output is a:4 2 1 8 10 8 10 In the original number group, ...

UNIQUE function in STL

Unique is one of the more practical and very common functions in STL; The role is that the container or array is heavy, but it is not complete; it is just put repeated elements behind the container, a...

More Recommendation

STL set and map containers set the sorting function: customize or use library functions

Set and map are easily associative containers, which can realize automatic sorting, and the sorting function among them defaults to sort operations in ascending order (or dictionary order). We can set...

C ++ stl unique () to heavy functions

In the STL, the Unique function is a de -heavy function. The function of Unique is to remove adjacent repetitive elements (only one). In fact, it does not really delete the repeated elements. It is st...

{A} + {B} STL unique in the use of

Give you two sets, required {A} + {B}. Note: There will be two identical elements of the same set. Input Each set of input data is divided into three lines, the first line has two numbers n, m (0 <...

C++STL standard template library functions-sort, swap, unique, lower_bound, upper_bound learning notes

Preface    I found that in addition to sort and swap, the STL library of C++ also comes with a lot of useful functions, and I have been switching to C++ for so long, but I don’t know. ( All beca...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top