C ++ vector deduplication

Unique functions of the STL role is to remove adjacent repeating elements

#include
#include
#include
using namespace std
int main()
{
int a[10] = {7,4,1,7,4,1,7,4,1,0};
sort(a,a+10);//小到大

vector<int>ver(a,a+10);

vector<int>::iterator iter = unique(ver.begin(),ver.end());
ver.erase(iter,ver.end());
for( int *t = ver.begin() ; t != ver.end() ; t ++ )
printf("%d ",*t);
return 0;
}
End result after sorting array is a [10] = {0,1,1,1,4,4,4,7,7,7};
Therefore the output section of the program is 0,147
 
 
In addition, the vector can be achieved without the sort of de-emphasis:
When the method is traversed, the equal elements except the first, all the latter mark. Such as: a sequence of characters, you may be labeled with numbers 0. It should be said straight 0 is assigned to this element. Then the other container, while traversing the original container, the elements do not Push_back tag to the new container.

Guess you like

Origin www.cnblogs.com/fanko/p/11277329.html