以下程序无法编译:
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <cstdlib>
#include <cmath>
void asort(std::vector<double>& v, std::function<bool(double, double)> f)
{
std::sort(v.begin(), v.end(), [](double a, double b){return f(std::abs(a), std::abs(b));});
}
int main()
{
std::vector<double> v({1.2, -1.3, 4.5, 2.3, -10.2, -3.4});
for (unsigned int i = 0; i < v.size(); ++i) {
std::cout<<v[i]<<" ";
}
std::cout<<std::endl;
asort(v, [](double a, double b){return a < b;});
for (unsigned int i = 0; i < v.size(); ++i) {
std::cout<<v[i]<<" ";
}
std::cout<<std::endl;
return 0;
}
因为 :
error : 'f' is not captured
这是什么意思以及如何解决问题?