unique函数的头文件为:
#include <algorithm>
unique函数是去掉相邻元素中的重复元素,把去掉的元素放在队尾。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string key;
while (cin >> key)
{
// sort(key.begin(), key.end());
// int n = unique(key.begin(), key.end())-key.begin();
unique(key.begin(), key.end());
for (int i = 0; i < key.length(); i++)
{
cout << key[i] << " ";
}
}
return 0;
}
从下面两组输出的对比,可以发现,确实只是去掉了相邻元素中的重复元素,并将去掉的元素放在队尾。