I've heard about lambda functions earlier, but I don't quite catch it.
If I have any function, it has its own name, so I can call it and get expected results.
However, it just doesn't make sense to me if function doesn't have any name. How do I access it? What is it good for? Is it useful? When should I use it, and can I bypass it with ordinary function/something else?
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
struct Record {
std::string name;
int data;
};
int main()
{
std::vector<Record> v = {{"John", 10}, {"Andrew", -1}, {"Mary", 15}};
std::vector<int> indices(v.size());
std::iota(indices.begin(), indices.end(), 0);
// sort indices in the order determined by the name field of the records:
std::sort(indices.begin(), indices.end(), [&](int a, int b) { return v[a].name < v[b].name; });
for(int idx: indices)
std::cout << v[idx].name << ' ' << v[idx].data << '\n';
}