Vector, Map, and Set
The Standard Template Library (STL) provides powerful container classes for storing and organizing data.
Example
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
using namespace std;
// Vector — dynamic array
vector<int> nums = {5, 3, 1, 4, 2};
nums.push_back(6);
nums.pop_back();
cout << nums.size() << endl; // 5
cout << nums[0] << endl; // 5
// Map — ordered key-value pairs
map<string, int> ages;
ages["Alice"] = 25;
ages["Bob"] = 30;
for (auto& [name, age] : ages) { // structured bindings (C++17)
cout << name << ": " << age << endl;
}
// Set — unique sorted elements
set<int> unique = {3, 1, 4, 1, 5, 9};
// Contains: {1, 3, 4, 5, 9} — sorted, no duplicates
// Unordered map — hash table (O(1) lookup)
unordered_map<string, string> env;
env["HOME"] = "/home/user"; 