How to Use Lambda function during Set Iteration in C++?

This tutorial will discuss multiple ways to use lambda function during set iteration in C++.

Table Of Contents

Using std::for_each() and Lambda Function

Suppose we have a set of integers. If we want to iterate over all the elements in this set and print them on the console in a single line, we can use the STL algorithm for_each() along with a lambda function.

std::set<int> numbers = {23, 45, 44, 21, 22, 60};

// Using std::for_each with a lambda
// to iterate overa set in C++
std::for_each(
    numbers.begin(),
    numbers.end(),
    [](int value) {
        std::cout << value << " ";
    });

Output:

21 22 23 44 45 60

In the for_each() algorithm, we provide two iterators: one pointing to the start of the range and another pointing to the end. The third argument is a lambda function. This lambda function takes a value as its argument and prints that value to the console. When we pass these three parameters to the for_each() function, it iterates over all the elements of the set and applies the given lambda function to each element. Inside this lambda function, we’re simply printing the value to the console. This allows us to iterate over all the elements of the set using the lambda function.

Using Range Based For Loop and Lambda Function

If you prefer to use a range-based for loop while still employing a lambda function, that’s possible too. First, declare the lambda function, which accepts a value as a parameter and then prints it to the console:

// A Lambda function
auto dataPrinter = [](const auto& value)
{
    std::cout << value << " ";
};

Next, use a range-based for loop to iterate over all the elements in the set. During each iteration, pass each value to the lambda function:

// Range-based for loop with lambda
for (const auto& value : numbers)
{
    dataPrinter(value);
}

Output:

21 22 23 44 45 60

This method allows us to iterate over all elements of the set using a range-based for loop and a lambda function.

Let’s see the complete example,

#include <iostream>
#include <algorithm>
#include <set>

int main()
{
    std::set<int> numbers = {23, 45, 44, 21, 22, 60};

    // Way 1

    // Using std::for_each with a lambda
    // to iterate overa set in C++
    std::for_each(
        numbers.begin(),
        numbers.end(),
        [](int value) {
            std::cout << value << " ";
        });

    std::cout << std::endl;


    // Way 2

    // A Lambda function
    auto dataPrinter = [](const auto& value)
    {
        std::cout << value << " ";
    };

    // Range-based for loop with lambda
    for (const auto& value : numbers)
    {
        dataPrinter(value);
    }
    std::cout << std::endl;

    return 0;
}

Output

21 22 23 44 45 60 
21 22 23 44 45 60

Summary

Today, we learned about use lambda function during set iteration in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top