64,646
社区成员
发帖
与我相关
我的任务
分享
words.erase(unique(words.begin(), words.end()), words.end());
就可以解决问题
#include "stdafx.h"
#include <numeric>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main()
{
string sa[]={"the", "quick", "red", "fox" ,"jumps", "over", "the" ,"slow", "red","turtle"};
vector<string> words(sa,sa+10);
vector<string>::iterator it;
for(it=words.begin();it!=words.end();it++)
cout<<*it<<"\t";
sort(words.begin(),words.end());
cout<<"调用sort以后排序显示为:"<<endl;
for(it=words.begin();it!=words.end();it++)
cout<<*it<<"\t";//调用sort排序是 fox jumps over quick red red slow the the turtle
//vector <string>::iterator ix=unique(words.begin(),words.end());
unique(words.begin(),words.end());//调用unique函数
cout<<"调用unique后排序显示为:"<<endl;
for(it=words.begin();it!=words.end();it++)
cout<<*it<<"\t";//调用unique后显示的是 fox jumps over quick red red slow the turtle turtle
return 0;
}