如何快速方便的输出向量vector容器中不重复的内容
2013-09-15 23:33
411 查看
在vector容器中,存入的内容难免会出现重复,那么如何快速输出或提前非重复的那些数据呢,即重复的数据只输出一次,直观的方法是每次输出都要通过循环比较是否已经输出过,这种方法还是比较费时的,可以利用unique函数简化代码,例子:
#include
"stdafx.h"
#include
<iostream>
#include
<vector>
#include
<algorithm>
#include
<string>
using namespace std;
int_tmain(int argc, _TCHAR* argv[])
{
strings;
unsigned
int size =0;
vector<string>v;
cout<<"输入个字符串:"<<endl;
while(size<5)
{
cin>>s;
v.push_back(s);
++size;
}
cout<<"输入的字符串为:"<<endl;
copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n"));
cout<<"排序后的结果为:"<<endl;
sort(v.begin(),v.end());
copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n"));
cout<<"不输出重复的结果:"<<endl;
vector<string>::iteratore = unique(v.begin(),v.end());
for(vector<string>::iterator b=v.begin(); b!=e;b++)
{
cout<<*b<<endl;
}
//再次输出排序后的结果
cout<<"再次输出排序后的结果:"<<endl;
copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n"));
return 0;
}
输出结果为:
#include
"stdafx.h"
#include
<iostream>
#include
<vector>
#include
<algorithm>
#include
<string>
using namespace std;
int_tmain(int argc, _TCHAR* argv[])
{
strings;
unsigned
int size =0;
vector<string>v;
cout<<"输入个字符串:"<<endl;
while(size<5)
{
cin>>s;
v.push_back(s);
++size;
}
cout<<"输入的字符串为:"<<endl;
copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n"));
cout<<"排序后的结果为:"<<endl;
sort(v.begin(),v.end());
copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n"));
cout<<"不输出重复的结果:"<<endl;
vector<string>::iteratore = unique(v.begin(),v.end());
for(vector<string>::iterator b=v.begin(); b!=e;b++)
{
cout<<*b<<endl;
}
//再次输出排序后的结果
cout<<"再次输出排序后的结果:"<<endl;
copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n"));
return 0;
}
输出结果为:
相关文章推荐
- 如何快速方便的输出向量vector容器中不重复的内容
- 利用copy函数简单快速输出/保存vector向量容器中的数据
- 利用copy函数简单快速输出/保存vector向量容器中的数据
- 如何快速交换两个向量容器中的内容
- 如何快速生成100万不重复的8位随机编号?
- 如何快速生成100万不重复的8位编号
- 如何快速生成100万不重复的8位编号 (转)
- ORALCE中如何使结果集中的重复值只输出一次
- 用Excel快速筛选出重复内容
- 如何在SQL Server中快速删除重复记录
- map等关联容器 vector等序列容器 如何防止迭代器失效 即erase()的使用
- STL 向量容器(vector)
- C++编程:用递归的方法输出vector对象的内容
- C++ STL入门教程(1)——vector(向量容器)的使用(附完整程序代码)
- C++ STL入门教程(1)——vector(向量容器)的使用(附完整程序代码)
- 如何快速生成100万不重复的8位编号
- vector<vector<string>> 二维向量遍历输出
- vector元素为自定义结构体类型时如何对容器元素进行排序?
- 视觉SLAM十四讲之向量(vector)与容器(unordered_map)的使用
- 如何用C++向量vector生成二维数组