C++ STL Cheatsheet

Quick reference for STL containers, algorithms, iterators, string utils, and helpers.

ItemDescriptionExampleCategory
vectorDynamic arrayvector<int> v = {1,2,3};Containers
arrayFixed-size arrayarray<int, 5> arr = {1,2,3,4,5};Containers
dequeDouble-ended queuedeque<int> dq;Containers
listDoubly linked listlist<int> li;Containers
stackLIFO containerstack<int> st;Containers
queueFIFO containerqueue<int> q;Containers
priority_queueMax heap (default)priority_queue<int> pq;Containers
setSorted unique elementsset<int> s;Containers
unordered_setHash table setunordered_set<int> us;Containers
mapKey-value sortedmap<int,string> mp;Containers
unordered_mapKey-value hashunordered_map<int,string> ump;Containers
begin(), end()Start & end iteratorfor(auto it=v.begin(); it!=v.end(); it++)Iterators
rbegin(), rend()Reverse iteratorsfor(auto it=v.rbegin(); it!=v.rend(); it++)Iterators
sort()Sort ascendingsort(v.begin(), v.end());Algorithms
reverse()Reverse rangereverse(v.begin(), v.end());Algorithms
max_element()Find max elementauto x = max_element(v.begin(), v.end());Algorithms
min_element()Find min elementauto x = min_element(v.begin(), v.end());Algorithms
count()Count occurrencescount(v.begin(), v.end(), 3);Algorithms
binary_search()Check existencebinary_search(v.begin(), v.end(), 5);Algorithms
lower_bound()1st ≥ elementlower_bound(v.begin(), v.end(), 5);Algorithms
upper_bound()1st > elementupper_bound(v.begin(), v.end(), 5);Algorithms
accumulate()Sum rangeaccumulate(v.begin(), v.end(), 0);Algorithms
pairStore 2 valuespair<int,string> p = {1,"a"};Utility
make_pair()Create pairauto p = make_pair(1,"a");Utility
tupleStore multiple valuestuple<int,string,double>Utility
tie()Unpack tupletie(a,b)=p;Utility
swap()Swap valuesswap(a,b);Utility
stringDynamic stringstring s = "hello";Strings
stoi()String → intint x = stoi("123");Strings
to_string()To stringstring s = to_string(10);Strings
sizeof()Size in bytessizeof(x)Others
iota()Fill increasing sequenceiota(v.begin(), v.end(), 1);Others