5.2.20C++ Programming

STL algorithms — sort, find, transform, accumulate, copy, all_of, any_of

2,110 words10 min readdifficulty · medium1 backlinks

The unifying idea: the iterator range [first, last)


1. sort — reorder a range


2. find — locate the first matching element


3. transform — map a function over a range


4. accumulate — fold a range into one value (<numeric>)


5. copy — duplicate a range


6 & 7. all_of / any_of — quantifiers over a range


Figure — STL algorithms — sort, find, transform, accumulate, copy, all_of, any_of

Recall Feynman: explain to a 12-year-old

Imagine a row of toy boxes. Instead of telling a robot "step from box 1 to box 5 and do stuff," you hand it a task card:

  • sort = "line them up smallest to biggest."
  • find = "point to the first red box; if there's none, point past the end."
  • transform = "paint each box and put the painted copy on a new shelf."
  • accumulate = "add up all the numbers on the boxes, starting from a number I give you."
  • copy = "make a duplicate row over here (make sure the shelf is big enough!)."
  • all_of = "are ALL boxes red?" any_of = "is ANY box red?" You just say the goal; the robot handles the walking. Less to get wrong, easier to read.

Flashcards

What range convention do nearly all STL algorithms use?
The half-open range [first, last)first included, last (one past end) excluded.
What does find return when the value is absent?
The last iterator you passed in (NOT nullptr).
Why must accumulate's init match the desired result type?
The accumulator type equals init's type; 0 (int) over doubles truncates each partial sum. Use 0.0.
What is the identity init for a product via accumulate?
1 (for sum it's 0).
Why can't you std::sort a std::list?
sort needs random-access iterators; list has bidirectional only — use list.sort().
What is the worst-case time of std::sort?
O(nlogn)O(n\log n) — introsort guarantees it (quicksort + heapsort fallback).
What does all_of return on an empty range, and why?
true — vacuous truth: x\forall x\in\varnothing holds (no counterexample).
What does any_of return on an empty range?
false — no element exists to satisfy the predicate.
How do you safely copy into an empty/auto-growing vector?
Use a back_inserter(dst) as the destination iterator.
What does transform return?
An iterator one past the last element written to the destination.
A lambda for sort returns true when…?
…its first argument should come BEFORE its second (a strict-weak-ordering "less-than").
How do you get the index of a find result it in a vector?
it - v.begin() (random-access iterators only).

Connections

  • Iterators and ranges — the abstraction every algorithm rides on.
  • Lambda expressions — how predicates/comparators are passed.
  • Function objects (functors) — alternative to lambdas for op/pred.
  • Time complexity Big-O — justifies the O(nlogn)O(n\log n) and O(n)O(n) costs.
  • std::vector — the most common container these run on.
  • Ranges library (C++20) — the modern, composable successor.

Concept Map

motivates

used by

used by

used by

used by

used by

used by

used by

uses

needs random-access iterators

uses predicate like

Iterator range first last half-open

Named generic tested loops

sort reorders range

find first match

transform maps elements

accumulate folds to one value

copy moves range

all_of every element true

any_of some element true

Returns last as not-found sentinel

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, STL algorithms ka core idea ek hi hai: tum kya karna chahte ho woh batao, kaise loop chalana hai woh STL handle karega. Har algorithm ek range [first, last) pe kaam karta hai — yaha first included hai aur last ek-step-aage, yaani excluded. Isi wajah se size nikalna easy hota hai (last - first) aur "nahi mila" ka matlab seedha last return karna — koi +1 ka jhanjhat nahi.

Saat important functions: sort (elements ko order me lagao, default chhote-se-bade, ya lambda se descending), find (pehla matching element ka iterator do, nahi mile to end()), transform (har element pe ek function lagao, jaise har number ka square), accumulate (<numeric> me, saare elements ko ek value me fold karo, jaise sum ya product — yaad rakho init ka type result ka type hai, isliye doubles ke liye 0.0 likho!), copy (ek range ko dusri jagah copy karo, par destination me jagah honi chahiye — warna back_inserter use karo), aur all_of / any_of (sab elements condition satisfy karte hai? ya koi ek karta hai?).

Common galtiyan: find ka result nullptr se compare karna (galat — end() se karo), accumulate me 0 daal ke doubles truncate ho jana, aur khali vector me copy karna (UB). In tino ka fix note me diya hai.

Yeh sab matter kyun karta hai? Kyunki competitive coding aur real projects me 80% data-crunching inhi 7 se ho jata hai. Hand-written loops me bug aate hai, ye tested aur fast hai (sort guaranteed O(nlogn)O(n\log n)). Naam padhte hi reader ko intent samajh aata hai — code clean aur professional banta hai.

Go deeper — visual, from zero

Test yourself — C++ Programming

Connections