← C++ Index

🔄 <algorithm> — Standard Algorithms — Sort, Search, Transform

Over 100 algorithms that work on any range of iterators. Combined with lambdas, this header eliminates most hand-written loops.

Include: #include <algorithm> | Namespace: std | Compile: g++ -Wall -std=c++17 ...

📋 Classes, Functions & Symbols

NameType / SignatureDescription
std::sortvoid sort(first, last [, cmp])Sort range ascending (O(n log n))
std::stable_sortvoid stable_sort(first, last [, cmp])Stable sort — preserves equal element order
std::findIt find(first, last, val)Iterator to first element == val
std::find_ifIt find_if(first, last, pred)Iterator to first element satisfying pred
std::binary_searchbool binary_search(first, last, val)True if val exists in sorted range
std::lower_boundIt lower_bound(first, last, val)Iterator to first element >= val
std::upper_boundIt upper_bound(first, last, val)Iterator to first element > val
std::countptrdiff_t count(first, last, val)Count occurrences of val
std::count_ifptrdiff_t count_if(first, last, pred)Count elements satisfying pred
std::transformOut transform(first, last, out, op)Apply op to each element
std::for_eachFn for_each(first, last, fn)Apply fn to every element
std::copyOut copy(first, last, out)Copy range to out
std::fillvoid fill(first, last, val)Fill range with val
std::reversevoid reverse(first, last)Reverse range in-place
std::uniqueIt unique(first, last)Remove consecutive duplicates
std::removeIt remove(first, last, val)Move non-val elements forward
std::remove_ifIt remove_if(first, last, pred)Remove elements satisfying pred
std::max_elementIt max_element(first, last)Iterator to maximum element
std::min_elementIt min_element(first, last)Iterator to minimum element
std::shufflevoid shuffle(first, last, rng)Randomly shuffle range (C++11)
std::any_ofbool any_of(first, last, pred)True if any element satisfies pred
std::all_ofbool all_of(first, last, pred)True if ALL elements satisfy pred
std::none_ofbool none_of(first, last, pred)True if NO element satisfies pred
std::clampconst T& clamp(val, lo, hi)Clamp val to [lo, hi] (C++17)

💡 Example Program

Copy, save as demo.cpp, and compile with the command at the bottom.

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <random>
using namespace std;

int main() {
    vector<int> v = {4, 2, 7, 1, 9, 3, 6, 5, 8};

    sort(v.begin(), v.end());
    cout << "Sorted: ";
    for (int x : v) cout << x << " ";
    cout << "\n";

    cout << "Has 7? " << (binary_search(v.begin(), v.end(), 7) ? "yes" : "no") << "\n";
    cout << "Count >5: " << count_if(v.begin(), v.end(), [](int x){ return x > 5; }) << "\n";
    cout << "Sum: "      << accumulate(v.begin(), v.end(), 0) << "\n";

    vector<int> sq(v.size());
    transform(v.begin(), v.end(), sq.begin(), [](int x){ return x * x; });
    cout << "Squares: ";
    for (int x : sq) cout << x << " ";
    cout << "\n";

    mt19937 rng(42);
    shuffle(v.begin(), v.end(), rng);
    cout << "Shuffled: ";
    for (int x : v) cout << x << " ";
    cout << "\n";
    return 0;
}
// Compile: g++ -Wall -std=c++17 -o algo_demo algo_demo.cpp

← <map>  |  🏠 Index  |  <fstream> →