{"id":20568737,"url":"https://github.com/agungdwiprasetyo/cheatsheet","last_synced_at":"2025-03-06T09:35:05.203Z","repository":{"id":92213824,"uuid":"86298458","full_name":"agungdwiprasetyo/cheatsheet","owner":"agungdwiprasetyo","description":"Cheatsheet STL C++, JavaScript, Python, ...","archived":false,"fork":false,"pushed_at":"2017-03-28T13:31:58.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-16T20:55:30.204Z","etag":null,"topics":["data-structures","stl"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/agungdwiprasetyo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-27T06:21:34.000Z","updated_at":"2020-10-13T03:10:00.000Z","dependencies_parsed_at":"2023-06-08T00:15:39.862Z","dependency_job_id":null,"html_url":"https://github.com/agungdwiprasetyo/cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agungdwiprasetyo%2Fcheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agungdwiprasetyo%2Fcheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agungdwiprasetyo%2Fcheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/agungdwiprasetyo%2Fcheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/agungdwiprasetyo","download_url":"https://codeload.github.com/agungdwiprasetyo/cheatsheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242181041,"owners_count":20085293,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-structures","stl"],"created_at":"2024-11-16T04:54:27.857Z","updated_at":"2025-03-06T09:35:05.171Z","avatar_url":"https://github.com/agungdwiprasetyo.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cheatsheet STL\n\n## C++\n### 1. Data structures for C++:\n\n### 1.1 Vector\n```#include \u003cvector\u003e ```\n\n```std::vector```\n\n**Use for**\n* Simple storage\n* Adding but not deleting\n* Serialization\n* Quick lookups by index\n* Easy conversion to C-style arrays\n* Efficient traversal (contiguous CPU caching)\n\n**Do not use for**\n* Insertion/deletion in the middle of the list\n* Dynamically changing storage\n* Non-integer indexing\n\n**Time Complexity**\n\n| Operation    | Time Complexity |\n|--------------|-----------------|\n| Insert Head  |          `O(n)` |\n| Insert Index |          `O(n)` |\n| Insert Tail  |          `O(1)` |\n| Remove Head  |          `O(n)` |\n| Remove Index |          `O(n)` |\n| Remove Tail  |          `O(1)` |\n| Find Index   |          `O(1)` |\n| Find Object  |          `O(n)` |\n\n### 1.2  Deque\n`#include \u003cdeque\u003e `\n\n`std::deque`\n\n**Use for**\n* Similar purpose of `std::vector`\n* Basically `std::vector` with efficient `push_front` and `pop_front`\n\n**Do not use for**\n* C-style contiguous storage (not guaranteed)\n\n**Notes**\n* Pronounced 'deck'\n* Stands for **D**ouble **E**nded **Que**ue\n\n### 1.3 List\n`#include \u003clist\u003e `\n\n`std::list`\n\n**Use for**\n* Insertion into the middle/beginning of the list\n* Efficient sorting (pointer swap vs. copying)\n\n**Do not use for**\n* Direct access\n\n**Time Complexity**\n\n| Operation    | Time Complexity |\n|--------------|-----------------|\n| Insert Head  |          `O(1)` |\n| Insert Index |          `O(n)` |\n| Insert Tail  |          `O(1)` |\n| Remove Head  |          `O(1)` |\n| Remove Index |          `O(n)` |\n| Remove Tail  |          `O(1)` |\n| Find Index   |          `O(n)` |\n| Find Object  |          `O(n)` |\n\n### 1.4 Map\n`#include \u003cmap\u003e `\n\n`std::map`\n\n**Use for**\n* Key-value pairs\n* Constant lookups by key\n* Searching if key/value exists\n* Removing duplicates\n* `std::map`\n    * Ordered map\n* `std::unordered_map`\n    * Hash table\n\n**Do not use for**\n* Sorting\n\n**Notes**\n* Typically ordered maps (`std::map`) are slower than unordered maps (`std::unordered_map`)\n* Maps are typically implemented as *binary search trees*\n\n**Time Complexity**\n\n**`std::map`**\n\n| Operation           | Time Complexity |\n|---------------------|-----------------|\n| Insert              |     `O(log(n))` |\n| Access by Key       |     `O(log(n))` |\n| Remove by Key       |     `O(log(n))` |\n| Find/Remove Value   |     `O(log(n))` |\n\n**`std::unordered_map`**\n\n| Operation           | Time Complexity |\n|---------------------|-----------------|\n| Insert              |          `O(1)` |\n| Access by Key       |          `O(1)` |\n| Remove by Key       |          `O(1)` |\n| Find/Remove Value   |              -- |\n\n### 1.5 Set\n`#include \u003cset\u003e `\n\n`std::set`\n\n**Use for**\n* Removing duplicates\n* Ordered dynamic storage\n\n**Do not use for**\n* Simple storage\n* Direct access by index\n\n**Notes**\n* Sets are often implemented with binary search trees\n\n**Time Complexity**\n\n| Operation    | Time Complexity |\n|--------------|-----------------|\n| Insert       |     `O(log(n))` |\n| Remove       |     `O(log(n))` |\n| Find         |     `O(log(n))` |\n\n### 1.6 Stack\n`#include \u003cstack\u003e `\n\n`std::stack`\n\n**Use for**\n* First-In Last-Out operations\n* Reversal of elements\n\n**Time Complexity**\n\n| Operation    | Time Complexity |\n|--------------|-----------------|\n| Push         |          `O(1)` |\n| Pop          |          `O(1)` |\n| Top          |          `O(1)` |\n\n### 1.7 Queue\n`#include \u003cqueue\u003e `\n\n`std::queue`\n\n**Use for**\n* First-In First-Out operations\n* Ex: Simple online ordering system (first come first served)\n* Ex: Semaphore queue handling\n* Ex: CPU scheduling (FCFS)\n\n**Notes**\n* Often implemented as a `std::deque`\n\n### 1.8 Priority Queue \n`#include \u003cpriority_queue\u003e`\n\n`std::priority_queue`\n\n**Use for**\n* First-In First-Out operations where **priority** overrides arrival time\n* Ex: CPU scheduling (smallest job first, system/user priority)\n* Ex: Medical emergencies (gunshot wound vs. broken arm)\n\n**Notes**\n* Often implemented as a `std::vector`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagungdwiprasetyo%2Fcheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fagungdwiprasetyo%2Fcheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fagungdwiprasetyo%2Fcheatsheet/lists"}