C++ Cheat Sheet
Template for competitive programming
#include <bits/stdc++.h>
using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); }
// Compile with C++17 and warnings
// g++ -std=c++17 -O2 -Wall main.cpp -o main
Input / output
int n; cin >> n;
vector<int> a(n); for (auto &x : a) cin >> x;
string line; getline(cin, line);
cout << x << " " << y << "\n";
cout << fixed << setprecision(6) << d << "\n";
Vector and string
vector<int> v(n, 0); v.push_back(x); v.pop_back();
v.size(); v.empty(); v.clear(); v.back();
sort(v.begin(), v.end()); reverse(v.begin(), v.end());
vector<vector<int>> g(n, vector<int>(m));
s.size(); s.substr(i, len); s.find("ab"); s += "x";
to_string(42); stoi("42"); stoll("9000000000");
Maps, sets, heaps
map<string, int> m; m["a"]++; m.count("a"); m.erase("a");
unordered_map<int, int> h; h.reserve(1 << 20);
set<int> s; s.insert(x); s.find(x) != s.end(); s.erase(x);
priority_queue<int> maxh;
priority_queue<int, vector<int>, greater<int>> minh;
stack<int> st; queue<int> q; deque<int> dq;
Algorithms
lower_bound(v.begin(), v.end(), x) - v.begin();
upper_bound(v.begin(), v.end(), x) - v.begin();
*max_element(v.begin(), v.end()); *min_element(v.begin(), v.end());
accumulate(v.begin(), v.end(), 0LL);
sort(v.begin(), v.end(), [](int a, int b) { return a > b; });
next_permutation(v.begin(), v.end());
__gcd(a, b); __builtin_popcount(x);
Classes
class A { public: A(int x) : x(x) {} virtual ~A() = default; private: int x; };
class B : public A { public: B() : A(0) {} };
auto p = make_unique<B>();
Full C++ documentation