Cheatsheet STL C++, JavaScript, Python, ...
#include <vector>
std::vector
Use for
Do not use for
Time Complexity
Operation | Time Complexity |
---|---|
Insert Head | O(n) |
Insert Index | O(n) |
Insert Tail | O(1) |
Remove Head | O(n) |
Remove Index | O(n) |
Remove Tail | O(1) |
Find Index | O(1) |
Find Object | O(n) |
#include <deque>
std::deque
Use for
std::vector
std::vector
with efficient push_front
and pop_front
Do not use for
Notes
#include <list>
std::list
Use for
Do not use for
Time Complexity
Operation | Time Complexity |
---|---|
Insert Head | O(1) |
Insert Index | O(n) |
Insert Tail | O(1) |
Remove Head | O(1) |
Remove Index | O(n) |
Remove Tail | O(1) |
Find Index | O(n) |
Find Object | O(n) |
#include <map>
std::map
Use for
std::map
std::unordered_map
Do not use for
Notes
std::map
) are slower than unordered maps (std::unordered_map
)Time Complexity
std::map
Operation | Time Complexity |
---|---|
Insert | O(log(n)) |
Access by Key | O(log(n)) |
Remove by Key | O(log(n)) |
Find/Remove Value | O(log(n)) |
std::unordered_map
Operation | Time Complexity |
---|---|
Insert | O(1) |
Access by Key | O(1) |
Remove by Key | O(1) |
Find/Remove Value | — |
#include <set>
std::set
Use for
Do not use for
Notes
Time Complexity
Operation | Time Complexity |
---|---|
Insert | O(log(n)) |
Remove | O(log(n)) |
Find | O(log(n)) |
#include <stack>
std::stack
Use for
Time Complexity
Operation | Time Complexity |
---|---|
Push | O(1) |
Pop | O(1) |
Top | O(1) |
#include <queue>
std::queue
Use for
Notes
std::deque
#include <priority_queue>
std::priority_queue
Use for
Notes
std::vector