#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(int a, int b)
{
return a < b;
}
int main()
{
vector<int> myvec{ 0, 20, 5, 7, 3, 1, 25 };
vector<int> lbvec(myvec);
sort(myvec.begin(), myvec.end(), compare);
cout << "predicate function:" << endl;
for (int it : myvec)
cout << it << ' ';
cout << endl;
sort(lbvec.begin(), lbvec.end(), [](int a, int b) -> bool { return a < b; }); // Lambda表达式
cout << "lambda expression:" << endl;
for (int it : lbvec)
cout << it << ' ';
cout << endl;
for each (int var in lbvec)
cout << var << ' ';
cout << endl;
for ( auto i:lbvec)
cout << i << " ";
cout << endl;
}
C++ 11 Lambda表达式和循环遍历初识
最新推荐文章于 2025-06-12 21:09:59 发布