0% found this document useful (0 votes)
9 views2 pages

Functions in CPP

All function in C++

Uploaded by

jonkhan101010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Functions in CPP

All function in C++

Uploaded by

jonkhan101010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ STL Cheat Sheet (Competitive Programming)

vector

push_back(x): Add element at end


pop_back(): Remove last element
size(): Get size of vector
empty(): Check if vector is empty
begin(), end(): Iterators to start and end
front(), back(): First and last element

stack

push(x): Push element on top


pop(): Remove top element
top(): Get top element
empty(): Check if stack is empty

queue

push(x): Add element at end


pop(): Remove front element
front(): Access front
empty(): Check if empty

priority_queue

push(x): Add element (heapify internally)


pop(): Remove top (max by default)
top(): Access top (largest element)
priority_queue<int, vector<int>, greater<int>>: Min-heap

set

insert(x): Insert element (auto sorted & unique)


erase(x): Remove element
find(x): Returns iterator to x or end()
count(x): 0 or 1 (since unique)
lower_bound(x): >= x
upper_bound(x): > x

map
C++ STL Cheat Sheet (Competitive Programming)

m[key] = val: Insert or update key-value


find(key): Iterator to key or end()
erase(key): Remove key
count(key): Returns 0 or 1

algorithm (header)

sort(begin, end): Sorts in ascending order


reverse(begin, end): Reverses elements
max_element(begin, end): Iterator to max
min_element(begin, end): Iterator to min
binary_search(begin, end, val): Returns true/false if found
lower_bound(begin, end, x): First >= x
upper_bound(begin, end, x): First > x

basic functions

abs(x): Absolute value


max(a, b), min(a, b): Maximum or minimum of two values
swap(a, b): Swap two variables
pow(x, y): x raised to the power y (returns double)
sqrt(x): Square root (returns double)
ceil(x), floor(x): Round up/down (returns double)
to_string(x): Convert number to string
stoi(s): Convert string to integer
getline(cin, s): Read full line into string s

You might also like