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 + +