Skip to content
UPEEC

STL Algorithms

sort, find, count_if, accumulate and friends — loops you don't have to write.

Intermediate
Comments 0
#include <algorithm>
#include <numeric>

std::sort(v.begin(), v.end());
auto it = std::find(v.begin(), v.end(), 42);
int big = std::count_if(v.begin(), v.end(), [](int x) { return x > 100; });
int sum = std::accumulate(v.begin(), v.end(), 0);

C++20 ranges make this shorter: std::ranges::sort(v);.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.