Templates and the standard library
Writing code once for many types with templates, and using the standard library's vector, map and algorithms instead of writing your own.
About 7 hours of study
Learning objectives
- Write function and class templates
- Explain template argument deduction
- Use vector and map
- Sort with the standard algorithms
Lessons
- 1
Templates
Write a function or class once for any type: the compiler makes an int version, a string version, and so on.
- 2
The standard library: vector, string, map and sort
Ready-made containers and algorithms: a vector grows by itself, a map keeps keys sorted, sort works on any range.
Module practice
- C++: One max for every typeEasy
- C++: A Stack class templateMedium
- C++: A sorted list for any typeHard
- C++: Sort and reverse a vectorEasy
- C++: Word counts with mapMedium
- C++: Sorting studentsMedium
- C++: A grade book with a map of vectorsHard