Open In App

Lambda Vs Binders in C++ STL

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The C++ programming language has features like lambda expressions and binders that let you build more compact and expressive code.  To make it simpler to build code that utilizes algorithms to modify data in containers, it is frequently used in conjunction with the STL algorithm library. Here we will discuss the major differences between Lambda and Binders in C++.

Lambda function

A lambda expression is a technique for instantly producing a function object—an object that may be called a function. To build anonymous functions in C++ lambda functions are used. These are functions without names or affiliations to identifiers. They are handy when you need to define a function inside of another function or send a little amount of functionality as an argument to a function, among other uses. Let us check with an example.

Example:

C++




// C++ Program to implement
// lambda expression
#include <algorithm>
#include <iostream>
#include <vector>
 
// Driver Code
int main()
{
    // Create a vector of integers
    std::vector<int> v = { 1, 2, 3, 4, 5 };
 
    // Use the for_each algorithm to square each element in
    // the vector
    std::for_each(v.begin(), v.end(),
                  [](int& n) { n *= n; });
 
    // Print the squared values
    for (int n : v)
        std::cout << n << ' ';
    std::cout << std::endl;
 
    return 0;
}


Output

1 4 9 16 25 

The vector v’s elements are iterated over using the for each algorithm in this example, and each element is squared using the lambda function [](int& n) n *= n; The lambda expression has no return value and only accepts the parameter int& (a reference to an integer) (the return type is inferred to be void). The lambda expression’s body only multiplies the argument by itself to square it.

Binders

The C++ Standard Library contains a series of functions called binders that can be used to attach one or more arguments to a function. By doing this, you can construct a new function object with some of its arguments already filled in, saving you from having to specify them each time the function is used. The std::bind function in C++ is used to build function objects known as binders. With the help of this function, you can build a function object that associates some of a function’s arguments with particular values, causing those arguments to automatically fill in when the function object is called. 

Example:

C++




// C++ Program to demonstrate
// the use of binders
#include <bits/stdc++.h>
 
// Driver code
int main()
{
    // Create a vector of integers
    std::vector<int> v = { 1, 2, 3, 4, 5 };
 
    // Create a function object that squares its argument
    auto square = std::bind(std::multiplies<int>(),
                            std::placeholders::_1,
                            std::placeholders::_1);
 
    // Use the for_each algorithm to square each element in
    // the vector
    std::for_each(v.begin(), v.end(), square);
 
    // Print the squared values
    for (int n : v)
        std::cout << n << ' ';
    std::cout << std::endl;
 
    return 0;
}


Output

1 2 3 4 5 

In this example, a binder is used to create a function object that squares its argument. The binder is created using the std::bind function, and it specifies that the std::multiplies function should be used to square the argument (by multiplying it by itself).

This illustration demonstrates the usage of binders to generate new function objects with some of their arguments already filled in. As a result, you won’t need to repeatedly supply the same arguments when writing code that regularly uses functions.

Difference between Lambda and Binders

These are the major difference between lambda and binders:

                               Lambda                                                    Binders
Used to create function objects                                                                                                                      Used to construct new function objects with some of their arguments filled in beforehand.
Can take arguments and have a return value Arguments are pre-filled, therefore when called, they cannot take more arguments.
Used frequently with STL algorithms to alter data in containers Used to make function calls simpler by removing the requirement to supply specific arguments each time.
Can reference member functions statically. Binders need pointers to reference the member function.
Syntax: capture -> return_type { function body } Syntax: std::bind(function, pre-filled_arg1, pre-filled_arg2, …)

In conclusion, lambda expressions and binders are both helpful tools that can make your code clearer and more expressive, but they have different applications. Binders are used to construct new function objects with part of their arguments already filled in, whereas lambda expressions are used to create function objects.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads