总时间限制:
1000ms
内存限制:
65536kB
// 在此处补充你的代码
描述
程序填空,产生指定输出
#include <cstdio> #include <iostream> #include <algorithm> #include <list> using namespace std; int main() { double a[] = {1.2,3.4,9.8,7.3,2.6}; list<double> lst(a,a+5); lst.sort(
); for(list<double>::iterator i = lst.begin(); i != lst.end(); ++i) cout << * i << "," ; return 0; }
输入
无
输出
9.8,7.3,3.4,2.6,1.2,
样例输入
无
样例输出
同输入
来源
Guo Wei
这里就是倒序输出链表。传入一个回调函数。共有三种写法
最简单的写法
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
bool comp(double x, double y) {
return x > y;
}
int main()
{
double a[] = { 1.2,3.4,9.8,7.3,2.6 };
list<double> lst(a, a + 5);
lst.sort(comp
// 在此处补充你的代码
);
for (list<double>::iterator i = lst.begin(); i != lst.end(); ++i)
cout << *i << ",";
return 0;
}
第二种写法,内置的Great函数功能与上面cmp函数相同
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
double a[] = { 1.2,3.4,9.8,7.3,2.6 };
list<double> lst(a, a + 5);
lst.sort(greater<double>()
// 在此处补充你的代码
);
for (list<double>::iterator i = lst.begin(); i != lst.end(); ++i)
cout << *i << ",";
return 0;
}
第三种写法
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
double a[] = {1.2,3.4,9.8,7.3,2.6};
list<double> lst(a,a+5);
lst.sort(
[](double a, double b) -> bool { return a > b; }
);
for(list<double>::iterator i = lst.begin(); i != lst.end(); ++i)
cout << * i << "," ;
return 0;
}