容器、泛型算法、和类是不是就是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. 关于Emit中动态类型TypeBuilder创建类标记的一点思考

      利用TypeBuilder是可以动态创建一个类型,现在有个需求,动态生成一个dll,创建类型EmployeeEx,需要继承原dll里面的Employee类,并包含Employee类上的所有类标记. ...

  2. asp.net缓存(三)

    Asp.net应用程序数据缓存 System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类.此命名空间包括 Cache 类,该类是一个字典,您可以在其中存储任意数据对象,如哈希表和 ...

  3. FZU1327 优先队列

    Problem 1327 Blocks of Stones II Accept: 318    Submit: 881Time Limit: 1000 mSec    Memory Limit : 3 ...

  4. (原+转)ubuntu中删除文件夹

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5638030.html 参考网址: http://zhidao.baidu.com/link?url=A ...

  5. C#读写word

    操作word之前需要在COM引入Microsoft Office 12.0 Object Library(文件库可能不一样) 然后添加using Microsoft.Office.Interop.Wo ...

  6. c++连接mysql数据库(使用mysql api方式,环境VS2013+MYSQL5.6)

    转载请注明出处,原文地址http://www.cnblogs.com/zenki-kong/p/4382657.html 刚开始写博客,博主还只是个大三汪,学艺不精,如有错误还请前辈指出(>^ω ...

  7. mysql创建存储过程

    -- 创建CREATE PROCEDURE proDelAccountById(IN in_accountid int) BEGIN -- 执行sql预计 END -- 调用 ; CALL proDe ...

  8. set up size, title to tcl tk main window

    #!/usr/bin/wish wm title . "this is main title" wm geometry . 500x300+30+200 500 --width 3 ...

  9. js判断数组和对象

    <script> var arr=new Array(); var obj={'1':2}; var num=11; function isType(obj){ if(obj instan ...

  10. 【转】ubuntu下putty的复制粘贴 -- 不错

    原文网址:http://www.nwber.com/?p=165 今天在ubutnu下想用putty玩玩,突然发现在windows里直接点击右键的复制居然不管用了,调设置也没有用.这可麻烦了,要是手动 ...