Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/agungdwiprasetyo/cheatsheet
Cheatsheet STL C++, JavaScript, Python, ...
https://github.com/agungdwiprasetyo/cheatsheet
data-structures stl
Last synced: about 1 month ago
JSON representation
Cheatsheet STL C++, JavaScript, Python, ...
- Host: GitHub
- URL: https://github.com/agungdwiprasetyo/cheatsheet
- Owner: agungdwiprasetyo
- Created: 2017-03-27T06:21:34.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-28T13:31:58.000Z (almost 8 years ago)
- Last Synced: 2024-11-16T04:54:53.619Z (3 months ago)
- Topics: data-structures, stl
- Language: C++
- Size: 13.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cheatsheet STL
## C++
### 1. Data structures for C++:### 1.1 Vector
```#include ``````std::vector```
**Use for**
* Simple storage
* Adding but not deleting
* Serialization
* Quick lookups by index
* Easy conversion to C-style arrays
* Efficient traversal (contiguous CPU caching)**Do not use for**
* Insertion/deletion in the middle of the list
* Dynamically changing storage
* Non-integer indexing**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)` |### 1.2 Deque
`#include ``std::deque`
**Use for**
* Similar purpose of `std::vector`
* Basically `std::vector` with efficient `push_front` and `pop_front`**Do not use for**
* C-style contiguous storage (not guaranteed)**Notes**
* Pronounced 'deck'
* Stands for **D**ouble **E**nded **Que**ue### 1.3 List
`#include ``std::list`
**Use for**
* Insertion into the middle/beginning of the list
* Efficient sorting (pointer swap vs. copying)**Do not use for**
* Direct access**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)` |### 1.4 Map
`#include ``std::map`
**Use for**
* Key-value pairs
* Constant lookups by key
* Searching if key/value exists
* Removing duplicates
* `std::map`
* Ordered map
* `std::unordered_map`
* Hash table**Do not use for**
* Sorting**Notes**
* Typically ordered maps (`std::map`) are slower than unordered maps (`std::unordered_map`)
* Maps are typically implemented as *binary search trees***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 | -- |### 1.5 Set
`#include ``std::set`
**Use for**
* Removing duplicates
* Ordered dynamic storage**Do not use for**
* Simple storage
* Direct access by index**Notes**
* Sets are often implemented with binary search trees**Time Complexity**
| Operation | Time Complexity |
|--------------|-----------------|
| Insert | `O(log(n))` |
| Remove | `O(log(n))` |
| Find | `O(log(n))` |### 1.6 Stack
`#include ``std::stack`
**Use for**
* First-In Last-Out operations
* Reversal of elements**Time Complexity**
| Operation | Time Complexity |
|--------------|-----------------|
| Push | `O(1)` |
| Pop | `O(1)` |
| Top | `O(1)` |### 1.7 Queue
`#include ``std::queue`
**Use for**
* First-In First-Out operations
* Ex: Simple online ordering system (first come first served)
* Ex: Semaphore queue handling
* Ex: CPU scheduling (FCFS)**Notes**
* Often implemented as a `std::deque`### 1.8 Priority Queue
`#include ``std::priority_queue`
**Use for**
* First-In First-Out operations where **priority** overrides arrival time
* Ex: CPU scheduling (smallest job first, system/user priority)
* Ex: Medical emergencies (gunshot wound vs. broken arm)**Notes**
* Often implemented as a `std::vector`