C++ Intermediate · Lesson 06 of 27
STL containers: vector, map, unordered_map, list
Choosing between the standard containers and their costs.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain stl containers: vector, map, unordered_map, list in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
std::vector is a resizable contiguous array and should be your default. std::map keeps keys sorted (O(log n) lookup), std::unordered_map hashes keys (average O(1) lookup, no order), and std::list is a doubly linked list that is rarely the best choice because it is slow to traverse.
Pick a container from how you use it. Call reserve() on a vector when you know the size, remember that push_back may reallocate (invalidating references), and prefer emplace_back to construct in place.
Guided lesson
Work through these steps
- 1.
Read the stl containers: vector, map, unordered_map, list example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
#include <vector>
// Resizable contiguous array.
#include <map>
// Sorted key-value container.
#include <unordered_map>
// Hash-based key-value container.
#include <list>
// Doubly linked list.
std::vector<double> angles;
// An empty list of angles.
angles.reserve(6);
// Allocate room for six values once, avoiding reallocations.
angles.push_back(0.5);
// Append a value at the end.
std::map<std::string, int> ids{{"elbow", 2}, {"base", 1}};
// A sorted map; iterating it visits 'base' before 'elbow'.
std::unordered_map<std::string, double> temps;
// A hash map for fast lookup by name.
temps["motor1"] = 41.5;
// Insert or update a key with operator[].
if (auto it = temps.find("motor2"); it == temps.end()) std::cout << "missing\n";
// find returns end() when the key is absent.
std::list<int> queue{1, 2, 3};
// A linked list; use only when you really need cheap insertion in the middle.What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
Watch it run · Values in memory
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for STL containers: vector, map, unordered_map, list. Expected result: 3 41.5 missing
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
int main() {
std::vector<double> angles;
// TODO: reserve room for 6 values, then push_back three angles
std::unordered_map<std::string, double> temps;
temps["motor1"] = 41.5;
std::cout << angles.size() << '\n' << temps["motor1"] << '\n';
// TODO: use find() to look up "motor2" and print "missing" when it is absent
return 0;
}Expected output
3
41.5
missingStuck? Show the full solution (try it yourself first)
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
int main() {
std::vector<double> angles;
angles.reserve(6);
angles.push_back(0.1);
angles.push_back(0.2);
angles.push_back(0.3);
std::unordered_map<std::string, double> temps;
temps["motor1"] = 41.5;
std::cout << angles.size() << '\n' << temps["motor1"] << '\n';
if (temps.find("motor2") == temps.end()) std::cout << "missing\n";
return 0;
}Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Store 1,000 motor temperatures in a vector with reserve, index them by name in an unordered_map, and compare timings for lookup in a vector, map and unordered_map. Explain the results.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.