C++ Tutorial

Containers

std::vector, std::list, std::map, std::set, and std::array

What are C++ Containers?

In C++, code is often the same for different data types. Instead of manually allocating memory for every collection of data, we use Containers from the Standard Template Library (STL). These are pre-written, highly optimized class templates that manage their own memory.
cpp
#include <vector>

// Dynamic array! 
std::vector<int> nums = {1, 2, 3};
nums.push_back(4); // Automatically grows!

Comparisons: Which one to use?

ContainerStructureBest Use Case...
std::vectorDynamic Array90% of the time! (Fast access, contiguous memory)
std::listDoubly Linked ListFast insertions/deletions anywhere.
std::mapBalanced Binary TreeKey-Value pairs (Ordered by key).
std::unordered_mapHash TableKey-Value pairs (Unordered, but FASTER).
std::setBalanced Binary TreeUnique items only.
std::arrayFixed-size ArrayWhen size is known at compile-time.

1. std::vector (The 90% Solution!)

A std::vector is a dynamic array that grows automatically. Because its memory is contiguous (all items are next to each other in RAM), it's the fastest container for almost every task.
cpp
std::vector<std::string> names;
names.push_back("Alpha");
names.emplace_back("Beta"); // Faster: Construct in-place!

// Access by index!
std::cout << names[0] << std::endl;

2. Associative: std::map and std::set

Maps allow you to associate a key with a value (like a dictionary), and Sets store only unique items.
cpp
#include <map>

std::map<std::string, int> scores;
scores["Alpha"] = 100;
scores["Beta"] = 50;

// Iterating over a map!
for (const auto& [name, score] : scores) {
    std::cout << name << ": " << score << std::endl;
}

3. Fixed-size: std::array (Modern C++)

Instead of using C-style arrays (int arr[3]), use std::array. It provides the same performance but adds safety features like .size() and .at().
cpp
#include <array>

std::array<int, 3> points = {10, 20, 30};
// points.push_back(40); // ERROR: Fixed size!

Summary

TypeExamplesDescription
vectorstd::vector<int>Dynamic, contiguous array
mapstd::map<k, v>Ordered Key-Value pairs
unordered_mapstd::unordered_mapFast Hash Table
setstd::set<int>Collection of UNIQUE items
liststd::list<int>Linked-list (no random access)
arraystd::array<int, 3>Fixed-size; no memory overhead
dequestd::deque<int>Fast additions to front AND back
Best ForData storage, retrieval, and organization
STLStandard Template Library (Massive speed boost!)
IterateUse the range-based for loop!
emplaceemplace_back is faster than push_back!
ComplexityO(1) for vector access; O(log n) for map search
ReserveUse vec.reserve(n) to avoid reallocations!
Clearcontainer.clear()
Emptycontainer.empty() (Faster than checking size!)
CapacityVector keeps extra space to avoid frequent growth
IteratorAll STL containers support iterators!
ModernC++20 added "Eraze-if" for cleaner removals
AlgorithmUse <algorithm> with your containers!
SafetyModern containers prevent memory leaks and overflows
PointerStoring smart pointers in containers is safe and useful
ReferenceBe careful with references to items (reallocations!)
GenericTemplates allow any object to be stored!
HashProvide std::hash to store custom keys in unordered_map
CompareMaps need < operator for custom key types
CostBalanced trees have more memory overhead than arrays