Featured image of post C++  STL Cheatsheet

C++ STL Cheatsheet

C++ Standard Template Library (STL) Cheatsheet

ConceptSyntax/ExampleDescription
Vectorsvector<int> v = {1, 2, 3};Creating a vector
Iteratorsvector<int>::iterator it = v.begin();Declaring an iterator
Listslist<int> myList = {1, 2, 3};Creating a list
Stacksstack<int> myStack;Creating a stack
Queuesqueue<int> myQueue;Creating a queue
Priority Queuespriority_queue<int> pq;Creating a priority queue
Setsset<int> mySet = {1, 2, 3};Creating a set
Mapsmap<string, int> myMap;Creating a map
Algorithmssort(v.begin(), v.end());Sorting a vector
findfind(v.begin(), v.end(), 2);Finding an element
countcount(v.begin(), v.end(), 2);Counting occurrences
accumulateaccumulate(v.begin(), v.end(), 0);Summing elements
fillfill(v.begin(), v.end(), 0);Filling elements with a value

STL Overview

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
STL
 ├── Containers
 │    ├── Sequence Containers
 │    │    ├── vector
 │    │    ├── list
 │    │    ├── deque
 │    │    └── array
 │    ├── Associative Containers
 │    │    ├── set
 │    │    ├── multiset
 │    │    ├── map
 │    │    └── multimap
 │    ├── Unordered Containers
 │    │    ├── unordered_set
 │    │    ├── unordered_multiset
 │    │    ├── unordered_map
 │    │    └── unordered_multimap
 │    └── Container Adapters
 │         ├── stack
 │         ├── queue
 │         └── priority_queue
 ├── Algorithms
 │    ├── Sorting
 │    │    └── sort
 │    ├── Searching
 │    │    └── find
 │    ├── Modifying
 │    │    └── copy
 │    ├── Non-modifying
 │    │    └── for_each
 │    └── Numeric
 │         └── accumulate
 ├── Iterators
 │    ├── Input Iterators
 │    ├── Output Iterators
 │    ├── Forward Iterators
 │    ├── Bidirectional Iterators
 │    └── Random Access Iterators
 └── Functors
      ├── Unary Functors
      ├── Binary Functors
      └── Function Objects

Key Components:

  • Containers: Hold collections of objects. Types include sequence containers (like vector and list), associative containers (like set and map), unordered containers (like unordered_map), and container adapters (like stack and queue).

  • Algorithms: Operate on containers (e.g., sort, find, copy, for_each, accumulate).

  • Iterators: Provide a way to traverse through the elements in a container (e.g., input, output, forward, bidirectional, random access iterators).

  • Functors (Function Objects): Objects that can be called as if they were functions.