sort()与拉姆达表达式的结合
#include
#include
#include
#include
#include #include
#include
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]=; 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
// //逆序
// 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.net/224327.html原文链接:https://javaforall.net
