STL初识

xiaoxiao2021-02-28  24

unique()函数是指“去掉”数组中重复的数字,但必须是有序数组,

而且所谓的去掉并不是真正去掉而是将重复的数字调到数组末位并返回一个不重复的数组末位位置;

#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { int n,q,a[max1]; while(scanf("%d",&n)==1&&n) { for(int i=0;i<n;i++)scanf("%d",&a[i]); sort(a,a+n);//这是STL中提供的排序函数,默认为升序。 int x=unique(a,a+n)-a;//这是STL中提供的删重函数,可以在有序 数列中将重复的数据移至数组末尾,并不是直接删除 for(int i=0;i<x;i++) { printf("%d ",a[i]); } } return 0; }
转载请注明原文地址: https://www.6miu.com/read-2629673.html

最新回复(0)