June 2018
Intermediate to advanced
348 pages
8h 45m
English
One of the major additions to the C++ language is Lambda functions and Lambda expressions. They are anonymous functions that the programmer can define at the call site to perform some logic. This simplifies the logic and code readability also increases in a remarkable manner.
Rather than defining what a Lambda function is, let us write a piece of code that helps us count the number of positive numbers in a vector<int>. In this case, we need to filter out the negative values and count the rest. We will use an STL count_if to write the code:
//LambdaFirst.cpp#include <iostream>#include <iterator>#include <vector>#include <algorithm>using namespace std;int main() { auto num_vect = vector<int>{ 10, 23, -33, 15, -7, 60, 80};Read now
Unlock full access