Dashboard/Block 1: Foundation/Module 5
Module 5Foundation

Vectors & the STL

So far, when you needed a list of values in C, you used arrays. And let's be honest... arrays in C are kind of a pain. You have to decide the size upfront, they don't know how long they are, and if you accidentally access an element that doesn't exist? No error message — just a crash or weird data corruption. Fun times.

This week, we're saying goodbye to all that nonsense and hello to std::vector — C++'s answer to "what if arrays were actually good?" A vector is basically a dynamic array that grows and shrinks whenever you need it to, always knows exactly how many elements it has, and can even warn you if you try to access something out of bounds.

We've got three big things to cover. First, vector basics — creating them, adding stuff with push_back, accessing elements, removing them. Second, range-based for loops — a clean, modern way to loop through everything in a container without dealing with indices. And third, STL algorithms — pre-built, battle-tested functions from the Standard Template Library that can sort, reverse, search, count, and sum your data with a single function call. Why write your own sort when the C++ experts already wrote one that's faster than yours would be?

FeatureC ArrayC++ Vector
SizeFixed at compile timeDynamic — grows and shrinks at runtime
Knows its length?No — you must track it separatelyYes — v.size() returns the count
Bounds checking?No — out-of-bounds access causes silent bugsOptional — v.at(i) throws an exception if i is invalid
Adding elementsNot possible (fixed size)v.push_back(value) adds to the end
Works with STL algorithms?Yes, but awkwardYes — naturally integrates with sort, find, etc.

The STL (Standard Template Library) is honestly one of the best things about C++. It gives you ready-made, heavily optimized data structures and algorithms so you don't have to reinvent the wheel. By the end of this week, you'll be managing collections of data like a pro.

Learning Objectives

Use std::vector for dynamic arrays (push_back, size, at, [])
Iterate with range-based for loops (auto, auto&)
Apply STL algorithms: sort, reverse, find, count, accumulate
Understand iterators at a basic level

Key Concepts

std::vector Basics

Range-Based For Loops

STL Algorithms