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()函数 ...
随机推荐
- 基础命名空间:序列化 System.Runtime.Serialization
对象通常都有状态(state),从一个对象中抽取这种状态,不论是将它存储于某地,还是通过网络传送,这种抽取动作称为“将一个对象序列化”,而反向处理过程,从一个被序列化的状态重建一个对象即为反序列化. ...
- FOR UPDATE SKIP LOCKED
SYS_UNIT 中有UNIT_ID 0000000001 0000000002 0000001100 原意为若启用多线程,则每个线程在获取时仅可以获取一条数据(互斥) 脚本如下 1.SELECT * ...
- C#控制条码打印机 纸张大小,间距,绘制内容(所有条码打印机通用)
其他条码知识 请访问:http://www.ybtiaoma.com ,本文仅供参考,请勿转载,谢谢 using System; using System.Drawing; using System. ...
- hdu1020Encoding
Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...
- (转) 学习C++ -> 指针初步
学习C++ -> 指针初步 一.指针 1. 什么是指针? 我们知道, 计算机的内存是由一个个独立的存储单元组成, 并且系统会对每一个存储单元分配一个唯一的号码, 称为这个存储 ...
- 关于安卓的log学习
什么时候会产生log文件? 1. 程序异常退出 Uncaused Exception. 2. 程序强制关闭 Force Closed(FC). 3. 程序无响应 Application No Resp ...
- firefox因gnash cpu 高
sudo apt-get remove --purge gnash 去adobe下载adobe flash for linux 解压 tar zxvf install_flash_player_11_ ...
- WPF笔记(1.6 数据绑定)——Hello,WPF!
原文:WPF笔记(1.6 数据绑定)--Hello,WPF! 这个一节都是在讲一个数据绑定的示例.功用:输入姓和名,点击Add按钮,ListBox增加一条记录,永远是字符串“name: nick”:L ...
- Qt编程之QtScript
需求是这样的: 需要给一个硬件接口测试些东西,用js来调用,js做成不同的独立模块分别测试不同的硬件接口,包括DMD内存,PCIE带宽等等.需要用一个exe来载入这些js文件分别调用测试模块.exe用 ...
- Clone Graph 解答
Question Clone an undirected graph. Each node in the graph contains a label and a list of its neighb ...