C++ Standard Template Library (STL) Cheatsheet
Concept | Syntax/Example | Description |
---|---|---|
Vectors | vector<int> v = {1, 2, 3}; | Creating a vector |
Iterators | vector<int>::iterator it = v.begin(); | Declaring an iterator |
Lists | list<int> myList = {1, 2, 3}; | Creating a list |
Stacks | stack<int> myStack; | Creating a stack |
Queues | queue<int> myQueue; | Creating a queue |
Priority Queues | priority_queue<int> pq; | Creating a priority queue |
Sets | set<int> mySet = {1, 2, 3}; | Creating a set |
Maps | map<string, int> myMap; | Creating a map |
Algorithms | sort(v.begin(), v.end()); | Sorting a vector |
find | find(v.begin(), v.end(), 2); | Finding an element |
count | count(v.begin(), v.end(), 2); | Counting occurrences |
accumulate | accumulate(v.begin(), v.end(), 0); | Summing elements |
fill | fill(v.begin(), v.end(), 0); | Filling elements with a value |
STL Overview
|
|
Key Components:
Containers: Hold collections of objects. Types include sequence containers (like
vector
andlist
), associative containers (likeset
andmap
), unordered containers (likeunordered_map
), and container adapters (likestack
andqueue
).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.