Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    The STL and professional C++ · Lesson 18 of 21

    Algorithms and lambdas

    sort, find_if, count_if and accumulate, with one-line functions.

    11 minutes3 games

    The <algorithm> header has dozens of ready-made functions that work on any container through iterators: begin() and end(). Instead of writing a loop each time, use them: quicker to write, well tested, and clearer to read.

    A lambda is a small unnamed function written right where it's needed: [](int x) { return x > 50; }.

    C++
    #include <algorithm>
    #include <iostream>
    #include <numeric>
    #include <string>
    #include <vector>
    
    struct Student {
        std::string name;
        int xp;
    };
    
    int main() {
        std::vector<Student> s = {{"Laila", 320}, {"Karim", 150}, {"Omar", 90}, {"Nour", 410}};
    
        std::sort(s.begin(), s.end(), [](const Student& a, const Student& b) { return a.xp > b.xp; });
        for (const auto& st : s) std::cout << st.name << " ";
        std::cout << "\n";
    
        int threshold = 100;
        auto above = std::count_if(s.begin(), s.end(), [threshold](const Student& st) { return st.xp > threshold; });
        std::cout << above << " above " << threshold << "\n";
    
        auto it = std::find_if(s.begin(), s.end(), [](const Student& st) { return st.name[0] == 'K'; });
        if (it != s.end()) std::cout << "found " << it->name << "\n";
    
        int total = std::accumulate(s.begin(), s.end(), 0, [](int sum, const Student& st) { return sum + st.xp; });
        std::cout << "total " << total << "\n";
        return 0;
    }
    PartMeaning

    []

    captures nothing

    [threshold]

    captures a copy of threshold

    [&total]

    captures total by reference (can change it)

    [=] / [&]

    everything it uses, by copy / by reference

    (int x)

    parameters

    { return … }

    body

    Important

    std::sort runs in O(n log n) and is not stable (equal elements may change order). Use std::stable_sort when the original order of equals matters. And beware: a [&] lambda that outlives the variables it captured (say, returned from a function) dangles.

    What's the output?

    #include <algorithm>
    #include <iostream>
    #include <vector>
    int main() {
        std::vector<int> v = {5, 2, 8, 2, 9};
        std::sort(v.begin(), v.end());
        auto last = std::unique(v.begin(), v.end());
        v.erase(last, v.end());
        for (int x : v) std::cout << x;
        std::cout << " " << v.size() << "\n";
    }

    What will this code print?

    Order the lines

    Order the code that sorts names by length, shortest first.

    1. Click the lines in the right order

    Interview question

    What is an iterator, and why do algorithms take iterators instead of containers?

    Exercise

    From {7, 3, 10, 4, 15, 8}: sort the numbers in descending order and print them on one line, then the count of even numbers on a second line.

    Expected output:
    15 10 8 7 4 3
    3
    C++
    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<int> v = {7, 3, 10, 4, 15, 8};
        
        return 0;
    }

    Learn more from

    • cppreference: Algorithms libraryen.cppreference.com
    • cppreference: Lambda expressionsen.cppreference.com
    • learncpp: Introduction to lambdaslearncpp.com

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousTemplatesNextExceptions
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.