Skip to content

The Standard Template Library (STL)

Programming C++ Difficulty 1/5

Beginning of the lesson

The Standard Template Library (STL)

Ready-made containers and algorithms, so you don't reinvent the wheel

Big Idea: The STL is a giant toolbox that already ships with C++. Instead of writing your own list, dictionary, or sorting code from scratch, you reuse containers like std::vector and std::map and algorithms like std::sort — tested, fast, and ready to use.
By the end you will be able to:
  • store a growing list of values in a std::vector,
  • look things up by key with a std::map,
  • use ready-made algorithms like sort, find, and count,
  • loop over any container with a clean range-based for.

1. What is the STL?

The Standard Template Library is a collection of ready-to-use tools built into C++. You get it for free — you just #include the right header. It has three main kinds of tools:

  • Containers — objects that hold your data (like vector and map).
  • Algorithms — ready-made operations that work on your data (like sort, find, count).
  • Iterators — the "pointers" that connect algorithms to containers (you'll see .begin() and .end()).
Think of the STL as a kitchen full of appliances. You could chop everything by hand, but why not use the blender that's already there?

2. std::vector — a list that grows

A plain C++ array has a fixed size. A std::vector is a smarter array that can grow and shrink while your program runs. You add items with push_back, ask how many there are with size(), and read any item with square brackets [i] (starting at index 0).

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> scores;      // an empty, resizable list of ints
    scores.push_back(90);    // add items to the end
    scores.push_back(75);
    scores.push_back(88);

    cout << "We have " << scores.size() << " scores\n";
    cout << "First score: " << scores[0] << "\n";
    return 0;
}

Output:

We have 3 scores
First score: 90

Notice we never said how big the vector was — it grew by itself as we pushed items in.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers