- Examples
Let's understand lambdas better by looking at a few examples.
We'll cover the following...
Lambdas with a vector #
Press + to interact
#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 listsstd::vector<std::string> myStrVec = {"12345", "123456", "1234", "1", "12", "123", "12345"};std::cout << "\n";// sorting with the functionstd::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 objectstd::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 functionstd::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 outputstd::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 returnstrue
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 ...