| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 705 人关注过本帖
标题:向朋友们请教一个简单的消重排序问题
只看楼主 加入收藏
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
 问题点数:0 回复次数:4 
向朋友们请教一个简单的消重排序问题

本来想给蜗牛加速解决排序问题的,就是输入10个数,消除其中重复的然后输出,我有了vector
为什么编链都没错,却不能达到目的?

请指教下。谢谢。



#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;


int main()
{
int str[10],j;
vector<int> vect;
vector<int>::iterator iter;
cout<<"Enter Data:";
for(j=0;j<10;j++)
{
cin>>str[j];
}
copy(str,str+10,vect.begin() );
iter=vect.begin();
unique(iter,iter+10);
for(j=0;j<10;j++)
{
cout<<"data are:"<<*iter++<<endl;
}
return 0;
}

搜索更多相关主题的帖子: str vector include begin int 
2006-03-23 21:20
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
you should first use sort(); then unique();

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-03-23 23:27
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
师妃暄,
单独使用
unique(); 并不一定能够实现消除重复项。 unique() 只消除临近相同项,所以配合使用 sort() 就能实现消除相同项,请注意一定要先使用 sort(), 然后再使用 unique()。
下面给你个DemoCode:
程序代码:

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <iterator>
using namespace std;


int main()
{
int nums[6] = {6, 2, 4, 2, 6, 5};
list<int> numList;
numList.insert(numList.begin(), nums, nums+6);
ostream_iterator<int, char> out(cout, \" \");
cout<<\"Before sort and unique:\n\";
copy(numList.begin(), numList.end(), out);
cout<<\"\nAfter sort and unique:\n\";
numList.sort();
numList.unique();
copy(numList.begin(), numList.end(), out);
cout<<endl;

system(\"pause\");
return 0;
}


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-03-24 01:07
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 

谢谢kai的提醒

可惜我把vector 改用list后

#include<iostream>
#include<algorithm>
#include<list>

using namespace std;


int main()
{
int str[10],j;
list<int> List;
list<int>::iterator iter;
cout<<"Enter Data:";
for(j=0;j<10;j++)
{
cin>>str[j];
}
copy(str,str+10,List.begin() );

List.sort();
List.unique();
iter=List.begin();
for(j=0;j<10;j++)
{
cout<<"data are:"<<*iter++<<endl;
}
return 0;
}

还是不行啊

请指点.


有实力才会有魅力 实力来自坚持不懈的努力
2006-03-24 09:19
xjc
Rank: 1
等 级:新手上路
帖 子:95
专家分:0
注 册:2004-12-2
收藏
得分:0 


int main()

{

int str[10],j;

cout<<"Enter Data:";
for(j=0;j<10;j++)
{
cin>>str[j];
}
list<int> List(str,str+j);
list<int>::iterator iter = List.begin();

List.sort();
List.unique();

for(;iter != List.end();)
{
cout<<"data are:"<<*iter++<<endl;
}

system("pause");

return 0;
}


时间是最宝贵的
2006-03-24 11:35
快速回复:向朋友们请教一个简单的消重排序问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015977 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved