容器、泛型算法、和类是不是就是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排序的更多相关文章

  1. 标准库中 vector list等排序

    1.list自带有排序函数sort():可以定义自己的排序规则,如: struct stTest { int count; wstring str; }; bool SortByNum(const s ...

  2. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

  3. STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase

    今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0         这个程序使用了vect ...

  4. 关于C++中vector和set使用sort方法进行排序

    C++中vector和set都是非常方便的容器, sort方法是algorithm头文件里的一个标准函数,能进行高效的排序,默认是按元素从小到大排序 将sort方法用到vector和set中能实现多种 ...

  5. 【转】vector中对象指针的排序

    原文:http://blog.csdn.net/tanlijun37/article/details/1948493 vector中对象指针的排序,初步想法是1: 把对象指针存到vector,重载bo ...

  6. C++ 中vector的基本用法

    //在网上看了好久,自己总结了一下下,第一篇博客,呼呼,学到不少 基本概念 vector容器是一个模板类,可以存放任何类型的对象).vector对象可以在运行时高效地添加元素,并且vector中元素是 ...

  7. C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET

    C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...

  8. STL中vector、list、deque和map的区别

    1 vector     向量 相当于一个数组    在内存中分配一块连续的内存空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capac ...

  9. 【转】STL中vector、list、deque和map的区别

    1.vector 向量 相当于一个数组 在内存中分配一块连续的内容空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capacity()函数 ...

随机推荐

  1. Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群

    本文的英文版本链接是 http://xuri.me/2013/11/20/install-mysql-cluster-on-ubuntu-12-04-lts.html MySQL Cluster 是 ...

  2. js倒计时 重发 效果

    <script type="text/javascript"> window.onload = function() { var wait = 60; function ...

  3. 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法【转自wjr2012的csdn blog】

    点击下载Genymotion-ARM-Translation.zip 将你的虚拟器运行起来,将下载好的zip包用鼠标拖到虚拟机窗口中,出现确认对跨框点OK就行.然后重启你的虚拟机.

  4. oc语言--语法

    一.OC简介 1.简介 它是C语言的基础上,增加了一层面向对象语法 OC完全兼容C语言 可以在OC代码中混入C语言代码,甚至是C++代码 可以使用OC开发mac OS X平台和IOS平台的应用程序 2 ...

  5. mysql中插入多条记录-微软批处理

    当向mysql中插入大量数据时,可以使用微软的批处理方式.这样可以防止内存溢出又提高了效率.我写了一个mysql中插入多条记录的例子.赋值代码可以直接使用. 1.首先需要添加两个dll MySql.D ...

  6. 子shell的$$

    http://blog.csdn.net/firefoxbug/article/details/7426109

  7. LeetCode_Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...

  8. arcgis api for silverlight

    原文 http://blog.sina.com.cn/s/blog_4638cf7b0100wntt.html arcgis api for silverlight(1) (2011-09-21 09 ...

  9. C语言的本质(37)——makefile之隐含规则和模式规则

    Makefile有很多灵活的写法,可以写得更简洁,同时减少出错的可能.本节我们来看看这样一个例子还有哪些改进的余地. 一个目标依赖的所有条件不一定非得写在一条规则中,也可以拆开写,例如: main.o ...

  10. bzoj1864 [Zjoi2006]三色二叉树

    Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. Sample ...