Topic 52 of 91 9. C++ Standard Template Library (STL) Interactive Tutorial

STL Algorithms in C++: Searching, Sorting, Modifying, and Processing Data

7 mins read Last updated: 26 Sep 2026 15,124 views

Direct Answer & Core Concept

STL algorithms in `` and `` are generic function templates that operate on sequences defined by iterator pairs `[first, last)`. They accept unary/binary callable predicates or lambdas to filter, transform, partition, search, and sort data with maximum compiler inlining efficiency.

Detailed Explanation & Mechanics

1. Algorithm Categories:

  • Non-modifying: std::find, std::count_if, std::all_of, std::any_of
  • Modifying: std::transform, std::replace, std::copy_if, std::reverse
  • Sorting / Partitioning: std::sort, std::stable_sort, std::partition
  • Numeric: std::accumulate, std::inner_product, std::iota
Architecture Diagram
ITERATOR RANGE PIPELINE:
[first] ──► element ──► Predicate / Transform ──► [last)

Runnable Code Example: STL Algorithms in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

int main() {
    std::vector<int> scores = {85, 42, 95, 78, 60, 91, 53};

    // 1. Counting with lambda predicate
    int highScorers = std::count_if(scores.begin(), scores.end(), [](int s) {
        return s >= 80;
    });
    std::cout << "Students with >= 80%: " << highScorers << "\n";

    // 2. Transforming data in-place
    std::transform(scores.begin(), scores.end(), scores.begin(), [](int s) {
        return s + 5; // Add 5 curve bonus points
    });

    // 3. Sorting in descending order
    std::sort(scores.begin(), scores.end(), std::greater<int>());

    std::cout << "Curved & Sorted Scores: ";
    for (int s : scores) std::cout << s << " ";
    std::cout << "\n";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What is the difference between `std::sort` and `std::stable_sort`?

A: `std::sort` uses Introsort ($O(N \log N)$ average) but does not preserve the relative order of equivalent elements. `std::stable_sort` preserves equal elements order, using merge sort with $O(N \log N)$ time.
Q

Q: Why are STL algorithms faster than passing function pointers in C?

A: In C++, template algorithms allow function objects and lambdas to be inlined directly at compile-time by the compiler, eliminating function call overhead.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

• Prefer std::ranges algorithms in modern C++20 (std::ranges::sort(vec)) for cleaner syntax.
• Avoid modifying containers (insert/erase) during an active algorithm traversal over that container.
• Use std::binary_search or std::lower_bound only on already sorted collections for $O(\log N)$ lookup.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.