C++ Standard Template Library (STL) | Containers, Iterators, Algorithms, Functors, Lambda Expressions
This complete tutorial on C++ Standard Template Library (STL) explains containers, iterators, algorithms, and function objects. It covers vectors, lists, maps, sets, stacks, queues, iterators, STL algorithms, and functors, lambda expressions, and custom comparators. Following best practices, it helps learners write efficient and reusable C++ code using STL.
Standard Template Library (STL) – Complete Tutorial
1. What is STL?
STL is a powerful library in C++ that provides generic classes and functions for data structures and algorithms.
- Consists of Containers, Iterators, Algorithms, and Function Objects
- Provides type-safe, reusable, and efficient code
2. Containers
Containers store collections of data. They are classified as:
a) Sequence Containers
- Store elements in linear order
- Examples:
vector,list,deque,stack,queue
Vector
List
Deque
- Double-ended queue
- Allows insertion/removal at both ends
Stack
- LIFO (Last In First Out)
Queue
- FIFO (First In First Out)
Priority Queue
- Max-heap or Min-heap
b) Associative Containers
- Store elements as key-value pairs or sorted sets
- Examples:
set,multiset,map,multimap
Set
Map
Multiset and Multimap
- Allow duplicate elements
Unordered Set and Map
- Use hash table, unsorted, faster lookup
3. Iterators
Iterators are objects that point to elements in containers.
Types of Iterators
- Input Iterator – read-only
- Output Iterator – write-only
- Forward Iterator – read/write, forward only
- Bidirectional Iterator – forward & backward
- Random Access Iterator – jump anywhere, e.g., vector
Iterator Operations
begin(),end(),rbegin(),rend()++it,--it,*itfor traversal
4. Algorithms
STL provides predefined algorithms:
Sorting
Searching
find(),binary_search(),count()
Numeric Algorithms
accumulate(),partial_sum(),iota()
Other Algorithms
reverse(),rotate(),max_element(),min_element()
5. Function Objects
Function objects (functors) allow objects to behave like functions.
Functors
Lambda Expressions
- Introduced in C++11 for inline anonymous functions
Custom Comparators
- Used in sort, priority_queue, set/map
Best Practices
- Use vectors for dynamic arrays
- Prefer iterators over indices for generic code
- Use associative containers for fast lookups
- Use lambda expressions for simple custom operations
- Avoid raw pointers; use STL containers for memory safety
Common Mistakes
- Using
erase()incorrectly with iterators - Modifying container while iterating (except carefully)
- Using wrong iterator type for algorithm
- Ignoring performance differences between containers
Summary
In this chapter, you learned about C++ STL, including:
- Sequence and associative containers
- Iterators and iterator operations
- Sorting, searching, and numeric algorithms
- Functors, lambda expressions, and custom comparators
STL allows writing efficient, reusable, and readable C++ code and is essential for competitive programming and real-world applications.