前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >sort用的什么排序算法_lambda表达式有什么组成

sort用的什么排序算法_lambda表达式有什么组成

作者头像
全栈程序员站长
发布2022-11-04 20:26:20
发布2022-11-04 20:26:20
31500
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是你们的朋友全栈君。

sort()与拉姆达表达式的结合

代码语言:javascript
代码运行次数:0
复制
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <map>
#include <utility>
#include <algorithm>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::map;
using std::sort;
struct Car{ 
int id,start,from,to;};
int main()
{ 

clock_t startTime=0,endTime=0;
startTime=clock();
map<int,int> MapRoot;
MapRoot[1]=1314520;
cout << MapRoot[1] <<endl;
string first="Test",last=" is ok";
auto PairTest=make_pair(first,last);
cout << PairTest.first<< PairTest.second<<endl;
vector<Car> c(1,{ 
2010,13,20,300});
c.push_back({ 
2011,12,24,400});
c.push_back({ 
2014,5,23,300});
c.push_back({ 
1994,9,22,210});
c.push_back({ 
1995,15,26,100});
int testA[]={ 
-2,100,20,111,-4,-2,1111};
int SizeA=sizeof(testA)/sizeof(int);
cout<<"排序前:"<<endl;
for (auto i:c)
{ 

cout<<i.id<<" "<<i.start<<" "<<i.from<<" "<<i.to<<" "<<" | ";
}cout<<endl;
//降序
sort(c.begin(),c.end(),[](Car &a,Car &b){ 
return a.start>b.start;});
// //升序
// sort(c.begin(),c.end(),[](Car &a,Car &b){return a.start<b.start;});
// //逆序
// sort(c.begin(),c.end(),[](Car &a,Car &b){return 1;});
// //正序
// sort(c.begin(),c.end(),[](Car &a,Car &b){return 0;});
cout<<"排序后:"<<endl;
for (auto i:c)
{ 

cout<<i.id<<" "<<i.start<<" "<<i.from<<" "<<i.to<<" "<<" | ";
}cout<<endl;
endTime=clock();
cout << "Run Time=" << (double)(endTime-startTime)<< "ms" << endl;
return 0;
}

运行结果

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/191776.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月19日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • sort()与拉姆达表达式的结合
  • 运行结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档