Topic 62 of 83
STL Intro
Overview
The STL is a massive collection of generic template classes providing data structures (Containers), Iterators, and Algorithms. It separates data from logic, allowing algorithms to work seamlessly across different containers.
Syntax
cpp
// The 3 Pillars of the STL:
// 1. Containers (vector, map, set...)
// 2. Iterators (Pointers that navigate containers)
// 3. Algorithms (sort, find, count...)
#include <vector>
#include <algorithm>
vector<int> v = {3, 1, 4}; // Container
sort(v.begin(), v.end()); // Algorithm using IteratorsCommon Pitfalls
- Not knowing STL. In competitive programming and interviews, rewriting a Queue or Hash Map from scratch instead of using the STL will cost you the job/contest.
Interview Tips
- Understand the architecture: Algorithms don't know about containers; they only know about iterators. This decoupling is the genius of the STL.
Real-World Example
A quick glimpse of STL power.
example
cpp
/*
Without STL: Writing a balanced binary search tree,
dynamic array resizing, and quicksort from scratch (1000+ lines).
With STL:
#include <map>
#include <vector>
#include <algorithm>
(Done in 3 lines).
*/