Das folgende Programm erfasst kompilieren nicht:Lambda einer Lambda: die Funktion nicht
#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;
}
weil:
error : 'f' is not captured
Was ist das und wie das Problem zu lösen?
Oder sogar erfassen Sie es durch Bezugnahme mit '[&]'. – Cameron
Ja, Sie haben Recht, ich habe meine Antwort aktualisiert. Aus irgendeinem Grund habe ich zu schnell gelesen und dachte, 'f' sei ein Funktionszeiger, kein Funktionsobjekt. – cdhowie