Junior — Middle
Have you worked with lambda functions before?
sobes.tech AI
Answer from AI
Yes, I have worked with lambda functions in C++. I used them for conveniently defining small functions directly at the call site, for example, for sorting or processing collections. Lambdas allow writing more compact and readable code.
Example of using a lambda function to sort a vector in descending order:
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<int> numbers = {5, 2, 9, 1, 5};
std::sort(numbers.begin(), numbers.end(), [](int a, int b) {
return a > b; // sort in descending order
});
for (int n : numbers) {
std::cout << n << " ";
}
return 0;
}