Search⌘ K

- Examples

Explore the use of lambda functions in C++ to simplify sorting and function definitions. Understand how to capture variables by copy or reference, utilize the 'this' binding, and create generic lambdas that work with various data types. This lesson helps you grasp core concepts of function declarations and lambda expressions to improve your C++ coding skills.

Lambdas with a vector #

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
bool lessLength(const std::string& f, const std::string& s){
return f.length() < s.length();
}
class GreaterLength{
public:
bool operator()(const std::string& f, const std::string& s) const{
return f.length() > s.length();
}
};
int main(){
// initializing with a initializer lists
std::vector<std::string> myStrVec = {"12345", "123456", "1234", "1", "12", "123", "12345"};
std::cout << "\n";
// sorting with the function
std::sort(myStrVec.begin(), myStrVec.end(), lessLength);
std::copy(myStrVec.begin(), myStrVec.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << "\n";
// sorting in reverse with the function object
std::sort(myStrVec.begin(), myStrVec.end(), GreaterLength());
std::copy(myStrVec.begin(), myStrVec.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << "\n";
// sorting with the lambda function
std::sort(myStrVec.begin(), myStrVec.end(), [](const std::string& f, const std::string& s){return f.length() < s.length();});
std::copy(myStrVec.begin(), myStrVec.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << "\n";
// using the lambda function for output
std::for_each(myStrVec.begin(), myStrVec.end(), [](const std::string& s){std::cout << s << ", ";});
std::cout << "\n\n";
}

Explanation

  • We have created a lessLength() function on line 7 that returns true if the first string is smaller than the second one in length.

  • This function can be used as the sorting criteria for std::sort on line 25. However, the lambda on line 35 performs the same task in a simpler way.

  • As we can see, the parameters of the lambda are the same as those of the defined function.

  • In line 30, the ...