c++ stl sort example
c++ stl sort函数使用举例:
#include <iostream>
#include<vector>
#include<algorithm>
#include<functional> using namespace std; class MyClass
{
public:
MyClass(int a,int b):first(a),second(b){}
int first;
int second;
bool operator <(const MyClass &m)const
{
return first<m.first;//重写“<”
}
}; bool LessSecond(const MyClass &m1,const MyClass &m2)
{
return m1.second<m2.second;
} int main()
{
vector<MyClass> vecMyclass;
int i=;
for(i=;i<=;i++)
{
MyClass m(-i,i*);
vecMyclass.push_back(m);
}
cout<<"before sort is:"<<endl;
for(i=;i<vecMyclass.size();i++)
cout<<vecMyclass[i].first<<","<<vecMyclass[i].second<<endl;
cout<<"after sort by first is:"<<endl;
sort(vecMyclass.begin(),vecMyclass.end());
for(i=;i<vecMyclass.size();i++)
cout<<vecMyclass[i].first<<","<<vecMyclass[i].second<<endl;
sort(vecMyclass.begin(),vecMyclass.end(),LessSecond);
cout<<"after sort by second is:"<<endl;
for(i=;i<vecMyclass.size();i++)
cout<<vecMyclass[i].first<<","<<vecMyclass[i].second<<endl;
return ;
}
共勉。
c++ stl sort example的更多相关文章
- STL sort 函数实现详解
作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...
- STL sort()函数
C++之所以得到这么多人的喜欢,是因为它既具有面向对象的概念,又保持了C语言高效的特点.STL 排序算法同样需要保持高效.因此,对于不同的需求,STL提供的不同的函数,不同的函数,实现的算法又不尽相同 ...
- STL::sort函数实现
声明:本文参考链接:STL::sort实现. 排序是面试中经常被问及的算法基础知识点,虽然实际应用中不会直接使用,但是理解这些简单的算法知识对于更复杂更实用的算法有一定的帮助,毕竟面试总不能问的太过深 ...
- STL sort 函数实现详解 ZZ
前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不对劲,知道自己回答错了.这几天特意看了一下,在 ...
- STL sort
STL的sort()算法,数据量大时采用Quick Sort,分段递归排序,一旦分段后的数据量小于某个门槛,为避免Quick Sort的递归调用带来过大的额外负荷,就改用Insertion Sort. ...
- c++ STL sort struct comp
详细解说 STL 排序(Sort) http://www.cppblog.com/mzty/archive/2005/12/15/1770.html 详细解说 STL 排序(Sort) 作者Winte ...
- 分享stl sort函数坑点导致coredump问题
在<Effective STL> 的条款21中就有讨论:永远让比较函数对相同元素返回false! 也就是说在实现stl sort函数自定义比较器时,一定要满足这种严格弱序化的问题.
- STL sort源码剖析
转载自:http://www.cnblogs.com/imAkaka/articles/2407877.html STL的sort()算法,数据量大时采用Quick Sort,分段递归排序,一旦分段后 ...
- STL——sort函数简介
参考:http://blog.csdn.net/s030501408/article/details/5329477 0)与C标准库qsort的比较:http://bbs.csdn.net/topic ...
随机推荐
- 商品录入功能v1.0【持续优化中...】
# 录入商品 def goods_record(): print("欢迎使用铜锣辉的购物商城[商品管理][录入商品]".center(30, "*")) whi ...
- FileWriter 中午乱码
解决办法 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (fil ...
- window 系统 修改服务器远程登录端口
window 系统 [ 默认3389远程端口 ] 快捷键:Ctrl+R 然后输入“regedit”,打开注册表 或者 单击左下角[开始]——[运行],然后在输入框输入 regedit,点击确定,打开 ...
- ajax加载菊花loading效果
Ajax异步请求的时候,一般都会利用一个动态的gif小图片来制作一个Ajax Loading,以便增加用户体验. 这里我们可以使用Spin.js,该js脚本压缩后5k,可以不用任何图片,任何外部CSS ...
- hive 存储格式及压缩
-- 设置参数 set hivevar:target_db_name=db_dw; use ${hivevar:target_db_name}; -- 创建textfile表 create table ...
- Why Nexiq 125032 USB Link Truck diagnostic tool is so helpful ?
As for as I am concerned , Heavy Duty Diagnostic Nexiq 125032 USB is a helpful tool , which has exce ...
- vue 之 nextTick 与$nextTick
VUE中Vue.nextTick()和this.$nextTick()怎么使用? 官方文档是这样解释的: 在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 D ...
- 安装tomcat时遇到的问题
1.刚开始在eclipse配置的tomcat是免安装的,后来提示 所以后来配置了一个安装版的. 2.后来运行server发现报错:8080,8005,端口被占用,然后关闭xammp上的server,然 ...
- python 之django (一) Windows环境下Django 1.6.11开发环境搭建(简易版)
转自 https://www.cnblogs.com/kkddij/p/4397163.html 需要安装如下部件: python 2.6.6 pip(最新版即可) Django 1.6.11 PyC ...
- Nuxt 2.3.X 配置babel
1. 在package.json中修改运行脚本 添加--exec babel-node 添加之后的效果为:(修改了8/10行) { "name": "nuxt-learn ...