#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers { 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 };
auto great_than_5_count = count_if(numbers.begin(), numbers.end(),
[](int x) { return (x > 5); });
cout << "The number of elements greater than 5 is: "
<< greater_than_5_count << "." << endl;
}
Note, count_if goes from the begin to end and calls a function that returns a bool.
• Instead of [], you’ll see things like [=], [&] and [this]
• These expression help the lambda expression “capture” variables within
the current scope
• [=] – make local variables available by copying them
• [&] – make local variables available as references
• [this] – make the current object’s data members available
• [var1, &var2] – capture var1 by copy and var2 by reference