C++ STL Cheat Sheet: A Comprehensive Guide
C++ Standard Template Library (STL) is a powerful tool that provides a rich set of templates and algorithms to facilitate generic programming. This C++ STL cheat sheet serves as a quick reference to help you understand and use STL containers, algorithms, and iterators effectively.
STL Containers
STL containers are used to store and manage data. They provide various types of containers like sequences, associative, and unordered containers.
Sequences
- Vector: Dynamic array with fast random access.
std::vector<T> - List: Doubly-linked list with fast insertion/deletion.
std::list<T> - Deque: Double-ended queue with fast insertion/deletion at both ends.
std::deque<T> - Forward List: Singly-linked list with fast insertion/deletion.
std::forward_list<T> - Array: Static array with contiguous memory allocation.
std::array<T, N>
Associative
- Set: Sorted unique elements.
std::set<T> - Multiset: Sorted elements with duplicates.
std::multiset<T> - Map: Sorted key-value pairs.
std::map<K, V> - Multimap: Sorted key-value pairs with duplicates.
std::multimap<K, V>
Unordered
- Unordered Set: Unordered unique elements.
std::unordered_set<T> - Unordered Multiset: Unordered elements with duplicates.
std::unordered_multiset<T> - Unordered Map: Unordered key-value pairs.
std::unordered_map<K, V> - Unordered Multimap: Unordered key-value pairs with duplicates.
std::unordered_multimap<K, V>
STL Algorithms
STL algorithms operate on sequences of elements and provide a wide range of functionality, such as sorting, searching, and manipulating data.

Sorting
- std::sort: Sorts a range of elements in ascending order.
- std::stable_sort: Sorts a range of elements in ascending order, preserving the relative order of equal elements.
- std::nth_element: Rearranges a range of elements such that the element at the specified position is in its final sorted position.
Searching
- std::find: Searches for the first occurrence of a value in a range.
- std::find_if: Searches for the first element satisfying a predicate in a range.
- std::binary_search: Checks if a sorted range contains a specific value.
Manipulation
- std::copy: Copies elements from one range to another.
- std::transform: Applies a function to each element in a range and stores the results in another range.
- std::remove: Removes elements from a range that satisfy a predicate.
- std::remove_if: Removes elements from a range that satisfy a predicate.
STL Iterators
STL iterators provide a way to access and manipulate elements in containers. They can be used to traverse, insert, and remove elements from containers.
Types of Iterators
- Input Iterator: Reads data and moves forward.
- Output Iterator: Writes data and moves forward.
- Forward Iterator: Reads and writes data, and moves forward.
- Bidirectional Iterator: Reads and writes data, and moves forward and backward.
- Random Access Iterator: Reads and writes data, and moves forward, backward, and can jump to any position.
Iterator Operations
| Operation | Description |
|---|---|
++it |
Increment the iterator. |
--it |
Decrement the iterator. |
it + n |
Add an integer to the iterator. |
it - it2 |
Subtract two iterators. |
it[n] |
Access the nth element from the iterator. |
*it |
Dereference the iterator to access the element it points to. |
This C++ STL cheat sheet provides a quick overview of STL containers, algorithms, and iterators. Familiarizing yourself with these tools will help you write efficient and maintainable C++ code. Happy coding!



















![SEO Tools collection [Technical SEO edition] Cheat Sheet](https://i.pinimg.com/originals/e0/f6/0c/e0f60c3d07d5b8d9f4712991a75e1614.jpg)




