Header Reference: <algorithm>

Header Reference: <algorithm>

Search, sort, partition, transform, set, and heap algorithms from the core STL algorithms header.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

What header pages are for

Header reference pages are meant to answer a practical question quickly: what this header provides, when to reach for it, and which usage rules are easiest to get wrong.

  • Start here when you already know roughly which header you need but want a fast operational summary.
  • Use the example section below as a minimal pattern, then adapt it to your real container, ownership, text, or concurrency workflow.
  • Jump to broader index pages when you need exhaustive coverage rather than a header-focused summary.

Header Reference: <algorithm>

Major algorithm families

Representative names

How to think about it

Caveats

Patterns worth recognizing

Example workflow

#include <algorithm>
#include <vector>

int main() {
	std::vector<int> values{5, 2, 5, 3, 1};
	std::sort(values.begin(), values.end());
	values.erase(std::unique(values.begin(), values.end()), values.end());
	return std::binary_search(values.begin(), values.end(), 3) ? 0 : 1;
}

Nearby pages

Minimal example

#include <algorithm>
#include <vector>

int main() {
    std::vector<int> values{4, 1, 3, 2};
    std::sort(values.begin(), values.end());

    auto it = std::find(values.begin(), values.end(), 3);
    return it == values.end() ? 1 : 0;
}

What to verify before relying on this header

  • What the primary facility is, and whether the surrounding code needs runtime behavior, compile-time support, or both.
  • Which constraints, preconditions, or complexity guarantees matter most for your use case.
  • Which neighboring headers you should review before locking in an API or implementation choice.