C++中vector和list排序
容器、泛型算法、和类是不是就是C++相对于C“++”的那部分呢?暂时先这么认为吧。如果这篇博客有幸被别人看到,请帮忙指出。——C++ 菜鸟 留。
vector的迭代器是随机访问迭代器,支持泛型算法的sort及其算法。
//vector排序
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric> using namespace std; //两个谓词函数
bool isShorter(const string &pre,const string &cur)
{
return (pre.size()<cur.size());
} bool GT4(const string &str)
{
return (str.size()>=4);
} int main()
{
cout<<"输入几个单词:"<<endl;
string str;
vector<string> strVec;
while (cin >> str)
{
strVec.push_back(str);
} //字典序
sort(strVec.begin(),strVec.end()); //去除重复的单词,返回一个迭代器,表示无重复的值得范围的结束
vector<string>::iterator iter_unique = unique(strVec.begin(),strVec.end()); //去除容器末尾重复的单词
strVec.erase(iter_unique,strVec.end()); sort(strVec.begin(),strVec.end(),isShorter);//按字符长短排序,相同长度的按字典序 cout<<"排序后的单词:"<<endl;
vector<string>::iterator iter = strVec.begin();
while (iter!=strVec.end())
{
cout << *iter<<"\t";
++iter;
}
cout<<endl; vector<string>::size_type cnt = count_if(strVec.begin(),strVec.end(),GT4);
cout<<cnt<<"个单词的长度大于等于4。"<<endl;
system("pause");
return 0;
}
其中unique函数返回一个迭代器,unique后需要调用erase手动删除容器尾部重复的单词。
而list容器上的迭代器是双向的,不支持随机访问,因此不能使用需要随机访问迭代器的sort算法。C++为list容器提供了特有的算法。
//list排序
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include <numeric> using namespace std; bool isShorter(const string &pre,const string &cur)
{
return (pre.size()<cur.size());
} bool GT4(const string &str)
{
return (str.size()>=4);
} int main()
{
cout<<"输入几个单词:"<<endl;
string str;
list<string> strList;
while (cin >> str)
{
strList.push_back(str);
}
strList.sort();//字典序
strList.unique();//去除重复的单词
strList.sort(isShorter);//按字符长短排序,相同长度的按字典序
cout<<"排序后的单词:"<<endl;
list<string>::iterator iter = strList.begin();
while (iter!=strList.end())
{
cout << *iter<<"\t";
++iter;
}
cout<<endl; list<string>::size_type cnt = count_if(strList.begin(),strList.end(),GT4);
cout<<cnt<<"个单词的长度大于等于4。"<<endl;
system("pause");
return 0;
}
在list的sort排序中,unique会自动去除重复的单词,程序员无需手动删除。
C++中vector和list排序的更多相关文章
- 标准库中 vector list等排序
1.list自带有排序函数sort():可以定义自己的排序规则,如: struct stTest { int count; wstring str; }; bool SortByNum(const s ...
- 【转】c++中Vector等STL容器的自定义排序
如果要自己定义STL容器的元素类最好满足STL容器对元素的要求 必须要求: 1.Copy构造函数 2.赋值=操作符 3.能够销毁对象的析构函数 另外: 1. ...
- STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase
今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0 这个程序使用了vect ...
- 关于C++中vector和set使用sort方法进行排序
C++中vector和set都是非常方便的容器, sort方法是algorithm头文件里的一个标准函数,能进行高效的排序,默认是按元素从小到大排序 将sort方法用到vector和set中能实现多种 ...
- 【转】vector中对象指针的排序
原文:http://blog.csdn.net/tanlijun37/article/details/1948493 vector中对象指针的排序,初步想法是1: 把对象指针存到vector,重载bo ...
- C++ 中vector的基本用法
//在网上看了好久,自己总结了一下下,第一篇博客,呼呼,学到不少 基本概念 vector容器是一个模板类,可以存放任何类型的对象).vector对象可以在运行时高效地添加元素,并且vector中元素是 ...
- C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET
C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...
- STL中vector、list、deque和map的区别
1 vector 向量 相当于一个数组 在内存中分配一块连续的内存空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capac ...
- 【转】STL中vector、list、deque和map的区别
1.vector 向量 相当于一个数组 在内存中分配一块连续的内容空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capacity()函数 ...
随机推荐
- 用IBM WebSphere DataStage进行数据整合: 第 1 部分
转自:http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0602zhoudp/ 引言 传统的数据整合方式需要大量的手工 ...
- mac上访问samba服务器
打开safari输入smb://ip,回车后出现输入用户名密码对话框,若是匿名则选择作为“客人”选项 例子 smb://192.168.2.3
- Joomla 二次开发 学习笔记
Joomla目录结构 /administrator 管理后台的路径 /cache 是缓存目录 /components 是组件(component)目录 /includes 是一个重要的目录,里面都是J ...
- shell之变量替换:临时替换
${FILE:-word} 若变量为空,给变量FILE添加一个临时默认值word,FILE本身值并不变化eg: FILE1=${FILE:-word} 若FILE为空,则赋予FILE1值word $ ...
- django中使用json.dumps处理数据时,在前台遇到字符转义的问题
django后台代码: import json ctx['dormitory_list'] = json.dumps([{", "is_checked": 1}, {&q ...
- 利用Azure Automation实现云端自动化运维(1)
Azure Automation是Azure上的一个自动化工作流引擎,基于Powershell,来帮助用户简化,集成和自动化Azure上的运维工作,例如: 实现定时开关虚拟机,节约成本 实现定时创建删 ...
- webservice 技术改进
Webservice 技术改进 1.不同系统不同语言之间的交互 基于http协议进行传输,使用REST服务实现WS 2.不同系统相同语言之间的交互 使用RPC(romate process call) ...
- SD card技术了解并WINCE下SDHC驱动开发(updated)
Suumary: 简单介绍了一下SD卡的历史和发展,同时结合MX31 ADS上的WINCE 下SDHC驱动更深入的了解该硬件的一些行为特点. 了解SD card SD是Secure Digital C ...
- Apache Commons Pool2 源码分析 | Apache Commons Pool2 Source Code Analysis
Apache Commons Pool实现了对象池的功能.定义了对象的生成.销毁.激活.钝化等操作及其状态转换,并提供几个默认的对象池实现.在讲述其实现原理前,先提一下其中有几个重要的对象: Pool ...
- UVA 100 - The 3n+1 problem (3n+1 问题)
100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...