STL Containers Programs
C++ STL container programs with output: vector, 2D vector, map, set, unordered_map, stack, queue, priority_queue, and deque, with when to pick each one.
10 programs · open one, then the next
Start with Vector Basics- 01Vector BasicsBeginnerLearn C++ vector basics: push_back, [] versus at(), front and back, size versus capacity, initializer lists, range-based for loops, and pop_back, with output.Open example →cpp#include <iostream> #include <vector> using namespace std;
- 02Vector OperationsIntermediateC++ vector operations with STL algorithms: sort, reverse, find with distance, insert and erase at an index, and clear. Learn the cost of each one.Open example →cpp#include <iostream> #include <vector> #include <algorithm> using namespace std;
- 032D VectorIntermediateBuild a 2D vector in C++: size it with resize, fill it with nested loops, initialize with nested braces, read matrix[i][j], and handle rows of different lengths.Open example →cpp#include <iostream> #include <vector> using namespace std;
- 04Map BasicsBeginnerC++ map basics with sorted keys: insert with [], read with at(), test keys with find and count, erase a key, and see why [] silently adds missing keys.Open example →cpp#include <iostream> #include <map> #include <string> using namespace std;
- 05Set BasicsBeginnerC++ set basics: unique, sorted values with insert, find, count, and erase, plus lower_bound and upper_bound to find the nearest value. Includes the exact output.Open example →cpp#include <iostream> #include <set> using namespace std;
- 06Unordered Map and SetIntermediateUse unordered_map and unordered_set in C++: hash tables with O(1) average lookups, unspecified order, ignored duplicates, buckets, and load factor.Open example →cpp#include <iostream> #include <unordered_map> #include <unordered_set> #include <string>
- 07Stack BasicsBeginnerC++ stack basics with LIFO order: push, top, pop, size, and empty, plus two classic uses: reversing a string and checking balanced parentheses, with output.Open example →cpp#include <iostream> #include <stack> using namespace std;
- 08Queue BasicsBeginnerC++ queue basics with FIFO order: push to the back, read front and back, pop from the front, and process a task list the same way BFS walks a graph.Open example →cpp#include <iostream> #include <queue> using namespace std;
- 09Priority QueueIntermediateC++ priority_queue explained: a max-heap by default, a min-heap with greater<int>, and a lambda comparator, with why the comparator direction feels backwards.Open example →cpp#include <iostream> #include <queue> #include <vector> using namespace std;
- 10Deque (Double-ended Queue)IntermediateC++ deque (double-ended queue) basics: push and pop at the front and back, index with [] and at(), insert and erase in the middle, and how it differs from vector.Open example →cpp#include <iostream> #include <deque> using namespace std;