0% found this document useful (0 votes)
10 views6 pages

CPP Code Boilerplate Complete

The document provides a comprehensive overview of C++ programming concepts, including boilerplate code, input/output methods, and data structures such as vectors, maps, and sets. It covers operations like sorting, searching, and manipulating containers, as well as advanced topics like binary search, prefix sums, and bit manipulation. Additionally, it includes examples of custom sorting and the use of priority queues, stacks, and queues.

Uploaded by

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

CPP Code Boilerplate Complete

The document provides a comprehensive overview of C++ programming concepts, including boilerplate code, input/output methods, and data structures such as vectors, maps, and sets. It covers operations like sorting, searching, and manipulating containers, as well as advanced topics like binary search, prefix sums, and bit manipulation. Additionally, it includes examples of custom sorting and the use of priority queues, stacks, and queues.

Uploaded by

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

German University of technology in Oman

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

cin >> x; // Input a variable


cout << x << "\n"; // Output a variable

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

Vectors (Dynamic Arrays)

vector<int> v; // Declare empty vector


v.pb(10); // Append element
v.insert(v.begin(), 5); // Insert at beginning
v.pop_back(); // Remove last element
v.erase(v.begin()); // Remove first element
v.clear(); // Remove all elements

sort(all(v)); // Sort ascending


sort(v.rbegin(), v.rend()); // Sort descending

// Unique Elements
sort(all(v));
v.erase(unique(all(v)), v.end());
German University of technology in Oman

Team Python++
12

reverse(all(v)); // Reverse the vector

if (find(all(v), x) != v.end()) cout << "Found";


int idx = distance(v.begin(), find(all(v), x)); // Get index of x

Maps (Key-Value Storage)

map<int, int> mp;


mp[1] = 100; // Insert or update
mp.emplace(2, 200); // Faster insert

if (mp.count(1)) cout << "Key found\n"; // Check existence


mp.erase(1); // Remove key
mp.clear(); // Clear all

for (auto &[key, val] : mp) cout << key << " " << val << "\n"; //
Fast loop

Sets (Unique Elements)

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";

Sorting & Searching

sort(all(v)); // Sort ascending


sort(v.rbegin(), v.rend()); // Sort descending

if (binary_search(all(v), x)) cout << "Found!\n"; // Works on sorted


arrays

auto lb = lower_bound(all(v), x); // First element >= x


auto ub = upper_bound(all(v), x); // First element > x
German University of technology in Oman

Team Python++
13

Sorting

sort(v.begin(), v.end()); // Sort in ascending order


sort(v.rbegin(), v.rend()); // Sort in descending order
sort(arr, arr + n); // Sort array in ascending order
sort(arr, arr + n, greater<int>()); // Sort array in descending order

Custom Sorting (Comparator)

sort(v.begin(), v.end(), [](int a, int b) { return a > b; }); //


Descending order using lambda
sort(v.begin(), v.end(), [](pair<int, int> a, pair<int, int> b)
{ return a.second < b.second; }); // Sort by second element of pair

Finding an Element

auto it = find(v.begin(), v.end(), x); // Returns iterator to x or


v.end() if not found
if (it != v.end()) cout << "Found at index " << it - v.begin();

Binary Search (For Sorted Containers)

if (binary_search(v.begin(), v.end(), x)) cout << "Found\n";

Min/Max Element in a Container

int mn = *min_element(v.begin(), v.end());


int mx = *max_element(v.begin(), v.end());
German University of technology in Oman

Team Python++
14

Counting Occurrences

int cnt = count(v.begin(), v.end(), x);

Unique Elements (Remove Duplicates)

sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());

Reverse a Container

reverse(v.begin(), v.end());

Prefix Sum

vector<int> pref(n + 1, 0);


for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + v[i - 1];

Lower Bound & Upper Bound (Sorted Containers)

auto lb = lower_bound(v.begin(), v.end(), x); // First element >= x


auto ub = upper_bound(v.begin(), v.end(), x); // First element > x

Next & Previous Permutations

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

map<int, int> mp;


mp[1] = 100;
mp[2] = 200;
if (mp.count(1)) cout << "Found\n";

Priority Queue (Heap)

priority_queue<int> pq; // Max heap


pq.push(10); pq.push(5); pq.push(20);
cout << pq.top(); // 20

priority_queue<int, vector<int>, greater<int>> pq_min; // Min heap

Stack & Queue

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 (Double-Ended Queue)

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;

You might also like