The STL is C++’s treasure chest - containers, iterators, and algorithms working in perfect harmony!”
<aside> <img src="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" alt="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" width="40px" />
vector, list, map, set, stack, queue)find, sort, copy, transform)find, count, search, equalcopy, transform, replace, fillsort, partial_sort, nth_elementmerge, union, intersection<aside> <img src="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" alt="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" width="40px" />
This module introduces the Standard Template Library (STL), focusing on templated containers, iterators, and algorithms. You’ll learn how these three pillars of the STL work together to create powerful, generic, and efficient C++ programs. Unlike previous modules, here we embrace the STL - using containers like vector, list, map, and algorithms from <algorithm> wherever appropriate.
</aside>
<aside> <img src="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" alt="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" width="40px" />
The Standard Template Library (STL) is C++’s collection of template classes and functions that provide common data structures and algorithms. It’s built on three fundamental concepts:
</aside>
<aside> <img src="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" alt="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" width="40px" />
vector, list, map, set, etc.)find, sort, copy, etc.)Think of the STL as a well-orchestrated symphony - containers hold the data, iterators provide access, and algorithms manipulate the data.
</aside>
<aside> <img src="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" alt="notion://custom_emoji/fec32b07-4245-472a-90f9-5ccb074f9fc1/18a39434-be44-808e-bf68-007a4c9cd9c6" width="40px" />
Containers are template classes that manage collections of objects. They provide different performance characteristics and interfaces for various use cases.
</aside>
Store elements in a specific order:
#include <iostream>
#include <vector>
#include <list>
#include <deque>
void demonstrateSequenceContainers() {
// Vector - dynamic array, fast random access
std::vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
std::cout << "Vector: ";
for (size_t i = 0; i < numbers.size(); ++i) {
std::cout << numbers[i] << " "; // Random access
}
std::cout << std::endl;
// List - doubly linked list, fast insertion/deletion
std::list<std::string> words;
words.push_back("Hello");
words.push_front("World"); // Efficient front insertion
words.push_back("STL");
std::cout << "List: ";
for (std::list<std::string>::iterator it = words.begin();
it != words.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
// Deque - double-ended queue
std::deque<char> letters;
letters.push_back('B');
letters.push_front('A'); // Efficient at both ends
letters.push_back('C');
std::cout << "Deque: ";
for (size_t i = 0; i < letters.size(); ++i) {
std::cout << letters[i] << " ";
}
std::cout << std::endl;
}
Store elements in a sorted order based on keys:
#include <iostream>
#include <map>
#include <set>
void demonstrateAssociativeContainers() {
// Map - key-value pairs, sorted by key
std::map<std::string, int> ages;
ages["Alice"] = 25;
ages["Bob"] = 30;
ages["Charlie"] = 22;
std::cout << "Ages map:" << std::endl;
for (std::map<std::string, int>::iterator it = ages.begin();
it != ages.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
// Set - unique elements, sorted
std::set<int> uniqueNumbers;
uniqueNumbers.insert(5);
uniqueNumbers.insert(2);
uniqueNumbers.insert(8);
uniqueNumbers.insert(2); // Duplicate, won't be added
std::cout << "Unique numbers: ";
for (std::set<int>::iterator it = uniqueNumbers.begin();
it != uniqueNumbers.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
Provide different interfaces to existing containers:
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
void demonstrateContainerAdapters() {
// Stack - LIFO (Last In, First Out)
std::stack<int> myStack;
myStack.push(1);
myStack.push(2);
myStack.push(3);
std::cout << "Stack (LIFO): ";
while (!myStack.empty()) {
std::cout << myStack.top() << " ";
myStack.pop();
}
std::cout << std::endl;
// Queue - FIFO (First In, First Out)
std::queue<std::string> myQueue;
myQueue.push("First");
myQueue.push("Second");
myQueue.push("Third");
std::cout << "Queue (FIFO): ";
while (!myQueue.empty()) {
std::cout << myQueue.front() << " ";
myQueue.pop();
}
std::cout << std::endl;
// Priority Queue - elements ordered by priority
std::priority_queue<int> pq;
pq.push(30);
pq.push(10);
pq.push(20);
std::cout << "Priority Queue (max first): ";
while (!pq.empty()) {
std::cout << pq.top() << " ";
pq.pop();
}
std::cout << std::endl;
}