Cheat Sheet
Cheat Sheet
Cheat Sheet
Go Green!
Data Structures
#include <bits/stdc++.h>
using II = long long int;
using III = unsigned long long int;
using namespace std;
#define MP make_pair
#define PB push_back
#define IN insert
#define P push
#define F first
#define S second
#define E erase
#define BG begin()
#define ED end()
#define SZ size()
#define NORM (200005)
#define MAXN INT32_MAX
#define T ::iterator
#define LOOP(i, n) for (II i = 0; i < n; i ++)
#define LOOP1(i, n) for (II i = 1; i <= n; i ++)
Data Structures
- Tuple
- Array
- Vector
- Set
- Multiset
- Unordered Set
- Pair
- Queue
- Priority Queue
- Map
- Multimap
- Unordered Map
- Stack
Tuple
1
tuple<char, int, float> t
t = make_tuple(’c’, 34, 2.3);
Vector
Set
Multiset
Priority Queue
2
Map
map<key, value> m;
m.insert({key, value});
m.count();
m.find();
Sort By Value
bool sortByVal(const pair<string, int> &a, const pair<string, int> &b) {
return (a.second < b.second);
}
count()
// checks if key 1 is present or not
if (mp.count(1)) cout << "The key 1 is present\n";
find()
// find() function finds the position at which 3 is present
for (auto itr = mp.find(3); itr != mp.end(); itr)
cout << itr->first << ’\t’ << itr->second << ’\n’;
Sort in Map
bool cmp(pair<string, int>& a, pair<string, int>& b)
return a.second < b.second;
for (auto& it : A) {
cout << it.first << ’ ’ << it.second << endl;
}
}
int main(){
map<string, int> M;
M = { { "GfG", 3 },{ "To", 2 },{ "Welcome", 1 } };
sort(M);
}
Decimal Places
3
cout << showpoint << fixed << setprecision() << "String" << endl;
Algorithms
Finding min/max element(s)’s index
int index = max_element(a.begin(), a.end());
4
To erase element
a.erase(a.begin(), index);
bool ok = true;
for (int ...){
ok = ok & (a[i] <= a[i + 1])
}
if (ok == true){
cout << "YES";
}
else cout << "NO";
bool isPrime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
5
Convert a number from base a to base b
// Initialize result
int num = 0;
// Update num
num += val(str[i]) * power;
// Update power
power = power * base;
}
return num;
}
6
// Function to return equivalent character of a given value
char reVal(int num)
{
if (num >= 0 && num <= 9)
return (char)(num + ’0’);
else
return (char)(num - 10 + ’A’);
}
// Update res
res += reVal(inputNum % base);
// Update inputNum
inputNum /= base;
}
return res;
}
7
void convertBase(string s, int a, int b)
{
// Convert the number from base A to decimal
int num = toDeci(s, a);
8
int sqrt(int x){
if (x == 0 || x == 1) return x;
int start = 1, end = x/2, ans;
while (start <= end) {
int mid = (start + end) / 2;
int sqr = mid * mid;
if (sqr == x) return mid;
return ans;
}
9
Finding G.C.D. of an array of numbers
if(result == 1)
{
return 1;
}
}
return result;
}
10
void find_prime(int n, bool prime[], bool primesquare[], int a[])
{
prime[1] = false;
int j = 0;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
a[j] = p;
primesquare[p * p] = true;
j++;
}
}
}
int countDivisors(int n)
{
if (n == 1) return 1;
int ans = 1;
for (int i = 0;; i++) {
if (a[i] * a[i] * a[i] > n) break;
int cnt = 1;
while (n % a[i] == 0){
n = n / a[i];
cnt = cnt + 1;
}
ans = ans * cnt;
}
if (prime[n]) ans = ans * 2;
else if (primesquare[n]) ans = ans * 3;
else if (n != 1) ans = ans * 4;
return ans;
} 11
Finding Number of Perfect Powers
return v.size();
}
Finding Determinant
12
// Dimension of input square matrix
#define N 4
// Function to get determinant of matrix
int determinantOfMatrix(int mat[N][N], int n)
{
int num1, num2, det = 1, index,
total = 1; // Initialize result
13
// traversing every row below the diagonal element
for (int j = i + 1; j < n; j++)
{
num1 = temp[i]; // value of diagonal element
num2 = mat[j][i]; // value of next row element
14