C++ STL — Standard Template Library
What is the STL?
The Standard Template Library (STL) is a collection of generic containers, algorithms, and iterators built into C++. It eliminates the need to write common data structures from scratch.
| Component | Examples |
|---|---|
| Sequence Containers | vector, list, deque, array, forward_list |
| Associative Containers | map, set, multimap, multiset |
| Unordered Containers | unordered_map, unordered_set |
| Container Adaptors | stack, queue, priority_queue |
| Algorithms | sort, find, count, transform, accumulate |
| Iterators | begin(), end(), rbegin(), rend() |
#include <iostream>
#include <map>
using namespace std;
int main() {
// map — sorted key-value pairs (O(log n) operations)
map<string, int> scores;
scores["Alice"] = 95;
scores["Bob"] = 87;
scores["Carol"] = 92;
// Access
cout << "Alice: " << scores["Alice"] << endl;
// Iterate (sorted by key)
for (const auto &[name, score] : scores) { // C++17 structured binding
cout << name << ": " << score << endl;
}
// Check existence
if (scores.count("Bob")) cout << "Bob exists" << endl;
// Erase
scores.erase("Bob");
cout << "Size after erase: " << scores.size() << endl; // 2
return 0;
}
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
// unordered_map — hash table, O(1) average operations
unordered_map<string, int> freq;
string words[] = {"apple", "banana", "apple", "cherry", "banana", "apple"};
for (const auto &w : words) freq[w]++;
for (const auto &[word, count] : freq) {
cout << word << ": " << count << endl;
}
// apple: 3, banana: 2, cherry: 1 (order not guaranteed)
return 0;
}
#include <iostream>
#include <set>
#include <stack>
#include <queue>
using namespace std;
int main() {
// set — unique sorted elements
set<int> s = {5, 3, 8, 3, 1, 5}; // duplicates removed
for (int n : s) cout << n << " "; // 1 3 5 8
cout << endl;
// stack — LIFO
stack<int> stk;
stk.push(1); stk.push(2); stk.push(3);
while (!stk.empty()) {
cout << stk.top() << " ";
stk.pop();
}
cout << endl; // 3 2 1
// queue — FIFO
queue<string> q;
q.push("first"); q.push("second"); q.push("third");
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
cout << endl; // first second third
// priority_queue — max-heap by default
priority_queue<int> pq;
pq.push(3); pq.push(1); pq.push(4); pq.push(1); pq.push(5);
while (!pq.empty()) {
cout << pq.top() << " ";
pq.pop();
}
cout << endl; // 5 4 3 1 1
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
vector<int> v = {5, 3, 8, 1, 9, 2, 7, 4, 6};
// sort
sort(v.begin(), v.end());
for (int n : v) cout << n << " "; // 1 2 3 4 5 6 7 8 9
cout << endl;
// binary_search (requires sorted)
cout << boolalpha << binary_search(v.begin(), v.end(), 7) << endl; // true
// find
auto it = find(v.begin(), v.end(), 5);
if (it != v.end()) cout << "Found 5 at index " << (it - v.begin()) << endl;
// count
vector<int> nums = {1, 2, 2, 3, 2, 4};
cout << "Count of 2: " << count(nums.begin(), nums.end(), 2) << endl; // 3
// accumulate (sum)
int sum = accumulate(v.begin(), v.end(), 0);
cout << "Sum: " << sum << endl; // 45
// transform — apply function to each element
vector<int> doubled(v.size());
transform(v.begin(), v.end(), doubled.begin(), [](int x){ return x * 2; });
for (int n : doubled) cout << n << " "; // 2 4 6 8 10 12 14 16 18
cout << endl;
// reverse
reverse(v.begin(), v.end());
for (int n : v) cout << n << " "; // 9 8 7 6 5 4 3 2 1
cout << endl;
return 0;
}