Templates and STL

Lesson 16 of 20

Write once, work for every type — templates stamp out versions while the STL ships ready-made containers.

The big picture

Generic power

Templates are recipes with a placeholder type; the STL is a giant toolbox built from exactly that idea.

template = recipe

You write the logic once; the compiler stamps out a real copy for each type you use.

typename T

T is the placeholder. Write T in the recipe, pass int or double at the call — the compiler fills it in.

STL containers

vector, map and set are all templates — std::vector<int> stamps the array for ints.

STL algorithms

sort, find and friends work on any container — generic code you never have to write.