C++ Standard Library · Lesson 1

The Standard Library at a Glance

A map of the whole toolkit: the handful of categories that hold the classes you'll actually use every day — and the one design idea that ties most of them together.

🎯 Reach for the right std:: type by reflex
The one idea

The standard library is enormous, but your daily surface is small and organized into a few categories. And the biggest one — the containers/algorithms world — is really just three things that snap together: containers hold data, iterators are a universal cursor over it, algorithms do the work through those cursors.

01 What "the standard library" actually is

Every conforming C++ compiler ships with a large library of ready-made types and functions, all living in the std namespace. You get at it with #include — one header per area — and refer to things as std::vector, std::string, and so on.

// pull in the pieces you need, then use them via std::
#include <vector>
#include <string>
#include <iostream>

int main() {
    std::vector<int> v = {3, 1, 2};   // a growable array of ints
    std::string name = "Ada";        // a real string, not char*
    std::cout << name << " has " << v.size() << " numbers\n";
}
Paste any code block in these lessons into godbolt.org (Compiler Explorer) to run it — no local toolchain needed.

It's mostly templates

Containers like vector<T> work for any element type T. You'll use templates constantly; you won't have to write them to be productive.

"Header-only" surprises

Most of the library is defined in headers, so there's nothing to link. #include the header and go.

"since C++NN"

The library grows every 3 years. We target C++17 and flag newer pieces (e.g. span since C++20). Your compiler picks the standard via -std=c++17 / c++20 / c++23.

💡 STL vs "the standard library" People say "STL" loosely to mean the whole standard library. Strictly, the STL is the original containers + iterators + algorithms design (§02). Everything else — strings, streams, smart pointers, threads — is "standard library" too. This course covers the popular pieces of all of it.

Source: cppreference — C++ Standard Library overview.

02 The big idea: containers + iterators + algorithms

The heart of the library is a deliberate split into three roles. Containers store your data. Algorithms (like sort, find) do generic work. They never touch each other directly — they meet through iterators, a common "cursor" interface that every container exposes via begin() and end().

CONTAINERS hold the data vector<int> map<K,V> list<T> ITERATORS a universal cursor begin() … end() [ half-open range ) ALGORITHMS do generic work sort(b, e) find(b, e, x) count(b, e, x) M containers × N algorithms, glued by one iterator interface → the library only needs M + N pieces, not M × N. That's the whole trick.
Iterators are the contract in the middle. Because sort speaks "iterator," it can sort a vector, a deque, or your own type — without knowing what they are.
// the three roles in one line each:
std::vector<int> v = {3, 1, 2};        // CONTAINER: holds the data
std::sort(v.begin(), v.end());          // ALGORITHM works over a range...
//             ^^^^^^^^^^^^^^^^^^^^^^         ...described by two ITERATORS
💡 Why you should care This is why the same std::sort works on almost everything, and why learning one algorithm pays off across every container. Lessons 7–8 dig into iterators and algorithms; for now just hold the shape in your head.

Source: cppreference — Algorithms library · Core Guidelines P.1 "express intent — prefer algorithms to hand-written loops".

03 The category map — the spine of this course

Here's the whole toolkit by category, with the headline classes in each and the lesson where we cover them. This table is the course. Bookmark it.

CategoryHeadline classesHeader(s)Lesson
Sequence containersvector, array, deque, list<vector> …2 · 6
Stringsstring, string_view<string>, <string_view>3
Associative (ordered)map, set, multimap<map>, <set>4 · 5
Unordered (hashed)unordered_map, unordered_set<unordered_map> …4
Container adaptorsstack, queue, priority_queue<stack>, <queue>6
Iterators & rangesiterator categories, ranges, views<iterator>, <ranges>7
Algorithmssort, find, transform, accumulate<algorithm>, <numeric>8
Function objectslambdas, std::function<functional>9
Smart pointersunique_ptr, shared_ptr, weak_ptr<memory>10
Vocabulary typespair, tuple, optional, variant<utility>, <optional> …11
I/O streamscout/cin, stringstream, fstream<iostream>, <sstream>, <fstream>12
Concurrencythread, mutex, atomic, future<thread>, <mutex> …13

Source: container categories per cppreference — Containers library. The full cheat sheet expands this with complexity and selection rules.

04 Two ideas that make the library behave: value semantics & RAII

Coming from a language with garbage collection and references-everywhere, two C++ habits explain almost all the "wait, why?" moments ahead.

Value semantics — objects own their stuff, copies are real copies

A std::vector isn't a handle to some heap object — it is the collection. Copy it and you get a full, independent copy. Pass it to a function by value and the function gets its own copy.

std::vector<int> a = {1, 2, 3};
std::vector<int> b = a;   // a DEEP copy — b has its own 1,2,3
b.push_back(4);              // a is still {1,2,3}; b is {1,2,3,4}
⚠ The flip side: accidental copies Because copies are deep, void f(std::vector<int> v) copies the whole vector on every call. Pass big objects by const& (const std::vector<int>&) to avoid it. Move semantics (Lesson 10) is the escape hatch when you want to transfer instead of copy.

RAII — a destructor cleans up when the object dies

Resource Acquisition Is Initialization: an object grabs a resource (memory, a file, a lock) in its constructor and releases it in its destructor — which runs automatically when the object goes out of scope. That's why you never free() a vector or close a std::ifstream by hand.

{
    std::vector<int> v(1000);  // allocates
    // ... use v ...
}   // <- v's destructor runs HERE, frees the memory. No leak, no free().
💡 The throughline Value semantics + RAII are why the standard containers are safe and leak-free by default — and why smart pointers (Lesson 10) exist: to give raw new/delete the same automatic cleanup. See the glossary for tight definitions of both.

Source: cppreference — RAII.

05 Your two everyday tools: cppreference & godbolt

You will not memorize the library, and you shouldn't try. Two skills replace memorization:

📖 Read cppreference

Every type/function has a page listing members, complexity, and the "(since C++NN)" tag. When unsure, search cppreference std::thing. Learn to skim the signature and the complexity line.

▶ Run it on godbolt

Compiler Explorer compiles + runs snippets in the browser. Set the language standard (e.g. -std=c++20) and paste. Every code block in this course is meant to be run there.

🎯 Try it now (2 minutes) Open godbolt.org, paste the very first snippet from §01, and run it. Seeing Ada has 3 numbers print is your tangible win for this lesson — and confirms your runner works for everything that follows.

06 Check yourself

In the STL design, what connects containers to algorithms?
After std::vector<int> b = a;, you do b.push_back(4). What happens to a?
Why don't you need to free a std::vector's memory yourself?
You need a growable list of numbers and haven't got a special reason otherwise. Which container is the default pick?

07 Flashcards

Q: What are the three roles in the STL's core design?
A: Containers hold data, algorithms do generic work, and iterators are the common cursor interface that lets any algorithm operate on any container. M + N pieces instead of M × N.
Q: What is "value semantics" in one line?
A: Standard objects are their data (not handles); copying is deep and independent. Pass big objects by const& to avoid accidental copies.
Q: What does RAII stand for, and what does it buy you?
A: Resource Acquisition Is Initialization: an object acquires a resource in its constructor and releases it in its destructor, which runs automatically at end of scope — so no manual free/close, no leaks.
Q: STL vs "the standard library" — what's the difference?
A: Strictly, the STL is the containers + iterators + algorithms design. The standard library is everything in std: that, plus strings, streams, smart pointers, threads, and more. Casually people use "STL" for all of it.
Q: Where do you look things up, and where do you run code?
A: Look up signatures + complexity on cppreference.com; run snippets on godbolt.org (Compiler Explorer). Don't try to memorize the library.
Q: What does "(since C++17)" on a cppreference page mean for you?
A: That feature only exists if you compile with that standard or newer (e.g. -std=c++17). This course is C++17-baseline and flags C++20/23 additions explicitly.
👩‍🏫 I'm your teacher — ask me anything

Stuck on why C++ copies things, or what a template angle-bracket actually means? Ask. Good warm-ups: "show me the M×N idea with a concrete second container", or "when is passing by value actually fine?" When you're ready, head to Lesson 2.