unique算法问题!

busyday115 2009-05-15 01:38:37
#include <numeric>
#include <iostream>
#include <algorithm>
#include <list>
#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);
for(vector<string>::iterator 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;
}
就是不明白为什么调用unique后显示的是这10个元素不是说相同的元素删除吗
为什么red两个还在里边!奇怪!
...全文
180 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Naruto 2011-04-06
  • 打赏
  • 举报
回复
unique 只是把重复的放在了后面而已。
beyond071 2009-05-15
  • 打赏
  • 举报
回复
unique的目的是去除“连续又重复”的元素,这也是之前为何要排序的缘故,它将相同的元素连续排列。
调用unique后,就破坏了这种连续性!
因此,在调用第二次unique时,实际返回的是一个无效的迭代器!所以erase想删除最后两个元素也无能为力了。
故,直接使用
words.erase(unique(words.begin(), words.end()), words.end()); 
就可以解决问题
lpf000 2009-05-15
  • 打赏
  • 举报
回复
unique,采取的是覆盖法,所以是那样的
lpf000 2009-05-15
  • 打赏
  • 举报
回复

#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;
}
lpf000 2009-05-15
  • 打赏
  • 举报
回复
给分 绝对是
lpf000 2009-05-15
  • 打赏
  • 举报
回复
哎呀我明白了,你的一个"red "多了义个空格 。。。。
beyond071 2009-05-15
  • 打赏
  • 举报
回复
楼主 你调用了2次unique,unique的作用是将不同的元素放置到返回的迭代器之前,他会对vector中的元素内容产生破坏。
只用一次就OK了。
busyday115 2009-05-15
  • 打赏
  • 举报
回复
就是去掉空格还是有问题
#include <numeric>
#include <iostream>
#include <algorithm>
#include <list>
#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);
for(vector<string>::iterator 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
words.erase(unique(words.begin(), words.end()), words.end());
cout<<"调用sease后显示为"<<endl;
for(it=words.begin();it!=words.end();it++)
cout<<*it<<"\t";
return 0;
}
最后输出的是
fox jumps over quick red slow the turtle the turtle
beyond071 2009-05-15
  • 打赏
  • 举报
回复
楼主 请注意 你的red一个后面有空格 一个后面没有空格 string可都能认出来哦,两个不一样啦
  • 打赏
  • 举报
回复
就是不明白为什么调用unique后显示的是这10个元素不是说相同的元素删除吗
为什么red两个还在里边!奇怪!

。。。汗,你应该理解unique类似于排序,他把相同的元素,并列排放而已,你要自己erase
busyday115 2009-05-15
  • 打赏
  • 举报
回复
#include <numeric>
#include <iostream>
#include <algorithm>
#include <list>
#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);
for(vector<string>::iterator 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
words.erase(unique(words.begin(), words.end()), words.end());
cout<<"调用sease后显示为"<<endl;
for(it=words.begin();it!=words.end();it++)
cout<<*it<<"\t";
return 0;
}
调用words.erase(unique(words.begin(), words.end()), words.end());
这个后显示的是fox jumps over quick red red slow the turtle 只是少了个turtle这个元素而已!各位是不是程序有问题!
beyond071 2009-05-15
  • 打赏
  • 举报
回复
unique返回的迭代器正是你要开始删除的区间的开始。
unique的作用只是把那些不重复的元素放在这个迭代器之前的区间内,这个区间之后的元素实际是乱的(由残留的重复元素和已经移走的元素组成)。
使用成员函数words.erase()删除unique(...)到words.end()这个区间内元素就OK了。
机智的呆呆 2009-05-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 barbara2008 的回复:]
必须调用erase进行删除words.erase(unique(words.begin(), words.end()), words.end());
[/Quote]
up,unique只把相同的放在容器的后面,而没有删除
barbara2008 2009-05-15
  • 打赏
  • 举报
回复
必须调用erase进行删除words.erase(unique(words.begin(), words.end()), words.end());
barbara2008 2009-05-15
  • 打赏
  • 举报
回复
unique 的size不变

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧