Cambo Freelance
ទំព័រដើមសេវាកម្មអត្ថបទការបង្រៀនក្រុមវគ្គបណ្តុះបណ្តាលទំនាក់ទំនង
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

ផ្ញើសាររបស់អ្នក

ត្រៀមចាប់ផ្តើមគម្រោងរបស់អ្នក? ទំនាក់ទំនងក្រុមរបស់យើង។

ទំនាក់ទំនង

សេវាកម្ម

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

តំណភ្ជាប់មានប្រយោជន៍

  • ទំព័រដើម
  • សេវាកម្ម
  • ការសិក្សា
  • អំពីយើង
  • ទំនាក់ទំនង
  • តម្លៃ

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Lambda Functions
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 43 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++AdvancedLesson 43 of 50CamboFreelanceJune 19, 2026

C++ Lambda Functions

Learn C++ lambda functions: anonymous functions, capture clauses (by value and reference), and use with STL algorithms.


Tutorials › C++ › Lambda Functions
Advanced9 min readLesson 43 of 50

What is a Lambda?

A lambda (C++11) is an anonymous, inline function — defined where you need it without naming it. Syntax:

[capture](parameters) -> returnType { body }
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    // Simple lambda
    auto greet = [](string name) {
        cout << "Hello, " << name << "!" << endl;
    };
    greet("Alice");   // Hello, Alice!

    // Lambda with return type
    auto add = [](int a, int b) -> int { return a + b; };
    cout << add(3, 5) << endl;   // 8

    // Lambda with STL
    vector<int> nums = {5, 2, 8, 1, 9, 3};
    sort(nums.begin(), nums.end(), [](int a, int b) { return a > b; }); // descending
    for (int n : nums) cout << n << " ";   // 9 8 5 3 2 1
    cout << endl;
    return 0;
}

Capture Clauses

Lambdas can capture variables from the surrounding scope:

int threshold = 5;

// Capture by value [=]
auto isAbove = [=](int n) { return n > threshold; };

// Capture by reference [&]
int count = 0;
auto countAbove = [&count, threshold](int n) {
    if (n > threshold) count++;
};

vector<int> data = {1, 7, 3, 9, 2, 6};
for (int n : data) countAbove(n);
cout << "Above " << threshold << ": " << count << endl;   // 3

Lambdas with STL

vector<string> words = {"banana", "apple", "cherry", "date"};

// Sort by length
sort(words.begin(), words.end(),
     [](const string& a, const string& b) { return a.size() < b.size(); });

// Filter with count_if
int shortWords = count_if(words.begin(), words.end(),
                          [](const string& s) { return s.size() <= 5; });
cout << "Short words: " << shortWords << endl;

Exercise

Given a vector of integers, use a lambda with remove_if and erase to remove all odd numbers. Print the result.

Show Solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    nums.erase(remove_if(nums.begin(), nums.end(),
               [](int n) { return n % 2 != 0; }), nums.end());
    for (int n : nums) cout << n << " ";   // 2 4 6 8 10
    cout << endl;
    return 0;
}

Quiz

  1. What does [&] in a lambda capture clause mean?

    • A) Capture nothing
    • B) Capture all variables by value
    • C) Capture all variables by reference
    • D) Capture only local variables
    Answer

    C) [&] captures all local variables by reference.

Summary

  • Lambdas are anonymous inline functions: [capture](params){ body }.
  • Capture by value with [=], by reference with [&].
  • Perfect with STL algorithms: sort, find_if, count_if, transform.
← Previous: Templates Next: Move Semantics →
c++lambdafunctionaladvanced
PreviousLesson 42: C++ TemplatesNextLesson 44: C++ Move Semantics
Back to All Tutorials