0. unique的常见用法详解优化-[STL模板]

0. unique的常见用法详解优化-[STL模板]

1.数组去重

#include<bits/stdc++.h>
using namespace std;
int a[10000];
int main() {
	int n,x;
	cin>>n;
	for(int i=0;i<n; i++) cin>>a[i];
	sort(a,a+n);
	x=unique(a,a+n)-a;
	for(int i=0; i<x; i++) cout<<a[i]<<" ";
	return 0;
}

输入:6
输入:1 2 5 4 2 2
输出:1 2 4 5


2.字符串去重

#include<bits/stdc++.h>
using namespace std;
int main() {
	string a;
	cin>>a;
	a.erase(unique(a.begin(),a.end()),a.end());
	cout<<a<<endl;
	return 0;
}

输入:l am      the best 
输出:l am the best

发布了44 篇原创文章 · 获赞 44 · 访问量 8142

猜你喜欢

转载自blog.csdn.net/xg987599519/article/details/104146520