LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 21 of 27

Lambda Expressions

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Programs & Examples

Lambda Expressions · Study material

An event bus with lambda handlers

Handlers are lambdas stored in std::function objects. Each captures the variable it updates by reference – safe here because all of them live in main as long as the bus does.

#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

class EventBus {
    std::map<std::string, std::vector<std::function<void(const std::string&)>>> handlers_;
public:
    void on(const std::string& event, std::function<void(const std::string&)> f) { handlers_[event].push_back(std::move(f)); }
    void emit(const std::string& event, const std::string& data) {
        for (auto& f : handlers_[event]) f(data);
    }
};

int main() {
    EventBus bus;
    int orders = 0;
    double revenue = 0;
    std::vector<std::string> log;

    bus.on("order", [&orders](const std::string&) { ++orders; });
    bus.on("order", [&revenue](const std::string& amount) { revenue += std::stod(amount); });
    bus.on("order", [&log](const std::string& amount) { log.push_back("order of " + amount); });
    bus.on("refund", [&revenue](const std::string& amount) { revenue -= std::stod(amount); });

    bus.emit("order", "499.50");
    bus.emit("order", "1200");
    bus.emit("refund", "499.50");
    std::cout << orders << " orders, revenue " << revenue << ", last: " << log.back() << '\n';
    return 0;
}

Output

2 orders, revenue 1200, last: order of 1200

Counters and ID generators with mutable lambdas

The mutable example shows how by-value captures act as private state that persists between calls, and how copying a lambda copies that state.

Recursive Fibonacci with deducing this

The recursion example compares C++23 deducing this, std::function and the "pass yourself" technique.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links