The use of unique (), unique_copy () in C + +

Source: Internet
Author: User

Unique and Unique_copy belong to the STL template of C + +, header file is algorithm

function:

Removes duplicate adjacent repeating elements, that is, if the two elements are the same but are not adjacent, it will not be removed

Unique will repeat the element is not deleted, but placed in the last side, it is how many elements, to go back to the number of elements, the preservation of the elements of culling

Unique_copy is to re-place the removed element into another array, without saving the excluded elements.

It is usually used before the array is sorted first

Usage:

Unique (begin, end), Begin is the start position, end is the ending position, returns an address,

Unique_copy (begin, end, Tar_begin), begin is the starting address of the original array, end address of the original array, Tar_begin is the starting address of the array to be copied to, that is, the destination array, and also returns an address

Example:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;typedef Long Long ll;ll K, p;int main () {    int a[] = {1, 2, 2, 2, 3, 3, 4, 5, 5, 3};     int b[] = {1, 2, 2, 2, 3, 3, 4, 5, 5, 3};    int len = Unique (A, a+10)-A;    printf ("Unique\nlen =%d\n", len);    for (int i = 0; i < i++)    {        if (i = = Len) puts ("# #");        printf ("%d", A[i]);    }    printf ("\ n below is unique_copy\n");    int c[10];    int d = unique_copy (b, b+10, c)-C;    printf ("Len =%d\n", d);    for (int i = 0; i < i++)    {        printf ("%d", C[i]);    }    printf ("\ n");    return 0;

The output is:

Unique
Len = 5
1 2 3 4 5 3 # # //The last output of 3 is because there is no 3 adjacent to him
4 5 5 3 //Description not deleted, just put on the back
Here's unique_copy.
Len = 6
1 2 3 4 5 3 2004323541 667813577-2 2004226402 //description deleted, because there are random numbers behind .

The use of unique (), unique_copy () in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.