Menu
Coddy logo textTech

Introducing std::vector

Part of the Logic & Flow section of Coddy's C++ journey — lesson 9 of 56.

You've been working with traditional C-style arrays, which have a fixed size that must be determined when you write your code. But what if you need a collection that can grow or shrink while your program is running? This is where std::vector from the Standard Template Library (STL) becomes invaluable.

A vector is essentially a dynamic array - it can automatically resize itself as you add or remove elements. Unlike regular arrays where you must specify the size upfront, vectors handle memory management for you, expanding automatically when you need more space (removing elements reduces the size, though the vector does not automatically release the underlying memory).

To use vectors in your program, you need to include the appropriate header at the top of your file:

#include <vector>

This flexibility makes vectors perfect for situations where you don't know how many elements you'll need in advance, or when the number of elements changes during program execution. Vectors provide the convenience of automatic memory management while still offering the performance and familiar syntax of arrays.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow