概述
lambda表达式用于定义并且创建匿名的函数对象。
格式:
[capture list](params list) mutable exception -> return type {function body}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compareFunc(int a, int b)
{
return a < b;
}
int main()
{
vector<int> v1{ 1,2,4,3 };
sort(v1.begin(),v1.end(),compareFunc);
for (auto i : v1)
cout << i << " ";
cout << endl;
vector<int> v2{ 1,4,6,2 };
sort(v2.begin(), v2.end(), [](int a, int b)->bool {return a < b; });
for (auto i : v2)
cout << i << " ";
return 0;
}