如何快速方便的输出向量vector容器中不重复的内容
来源:互联网 发布:java 数据字典设计 编辑:程序博客网 时间:2024/05/24 04:34
在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;
}
输出结果为:
- 如何快速方便的输出向量vector容器中不重复的内容
- vector向量容器的删除
- vector向量容器的一些基本操作
- C++向量容器vector的使用
- 如何快速交换两个向量容器中的内容
- 统计vector容器中重复元素的个数
- c++中 的向量vector
- c++中 的向量vector
- 利用copy函数简单快速输出/保存vector向量容器中的数据
- 如何对EXCEL一列的数据中快速提取不重复的记录
- 较好的vector容器输出代码片段
- vector容器的内存分配——vector向量容器(补充)——STL Introduction
- vector向量容器中使用insert()方法
- flex布局中,保持内容不超出容器的解决办法
- 统计一个区域中不重复的单元格内容数量
- java中向量类Vector的使用
- C++中vector向量的基本操作
- c++中vector向量的使用
- What is a "sosreport" and how do I create it in Red Hat Enterprise Linux 4.6 and later
- 基于移动终端(平板电脑、手机)的网络调查
- struct sizeof
- How to interpret the meanings of HDD LEDs
- 图像分割方法
- 如何快速方便的输出向量vector容器中不重复的内容
- SUN X4200 Server, All Leds of HDD0 is not lit
- weblogic 部署应用时一直停留在部署状态解决办法
- Oracle Licensing
- How to clear the FMA faults and error logs from Solaris
- 题目:请给出一个运行时间为Θ(nlgn)的算法,使之能在给定一个由n个整数构成的集合S和另一个整数x时,判断出S中是否存在有两个其和等于x的元素。
- J2ME主要应用于嵌入是系统开发
- Redhat Enterprise Linux 系列笔记1
- How to Clear Fault from ilom CLI