CPP Code Boilerplate Complete
CPP Code Boilerplate Complete
Team Python++
11
Boilerplate Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define fast_io ios::sync_with_stdio(false); cin.tie(0);
int main() {
fast_io;
// Your code here
return 0;
}
Input / Output
vector<int> v(n);
for (int &x : v) cin >> x; // Fast vector input
int a, b;
cin >> a >> b; // Multiple input
cout << a << " " << b << "\n"; // Output multiple values
// Unique Elements
sort(all(v));
v.erase(unique(all(v)), v.end());
German University of technology in Oman
Team Python++
12
for (auto &[key, val] : mp) cout << key << " " << val << "\n"; //
Fast loop
set<int> s;
s.insert(5); // Insert
s.erase(5); // Remove
cout << *s.begin(); // Get min element
cout << *s.rbegin(); // Get max element
if (s.count(5)) cout << "Exists\n";
Team Python++
13
Sorting
Finding an Element
Team Python++
14
Counting Occurrences
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
Reverse a Container
reverse(v.begin(), v.end());
Prefix Sum
next_permutation(v.begin(), v.end());
prev_permutation(v.begin(), v.end());
German University of technology in Oman
Team Python++
15
Set Operations
set<int> s;
s.insert(3); // Insert element
s.erase(3); // Erase element
s.count(3); // Check if exists
s.find(3); // Get iterator to element (s.end() if not found)
Map Operations
stack<int> st;
st.push(5); st.push(10);
st.pop(); cout << st.top();
queue<int> q;
q.push(5); q.push(10);
q.pop(); cout << q.front();
German University of technology in Oman
Team Python++
16
deque<int> dq;
dq.push_front(1); dq.push_back(2);
dq.pop_front(); dq.pop_back();
Bit Manipulation
int x = 5; // 0101
x = x << 1; // Left shift (multiply by 2)
x = x >> 1; // Right shift (divide by 2)
x = x & (x - 1); // Turn off rightmost 1-bit
bool isPowerOfTwo = (x & (x - 1)) == 0;