Learning C++ can be overwhelming. It is easy to forget the STL functions, syntax, and memory tricks for beginners. If you are looking for a C++ cheatsheet that helps you save time and prevent common errors, this article is for you. This cheat sheet contains all the information you need, ranging from basic looping statements to complex data structures. With this resource, coding in contests and practice sessions becomes simpler.
Basic Syntax and Fast I/O in C++ Cheatsheet
Every C++ program starts with a structure. For competitive programming, #include <bits/stdc++.h> loads all standard libraries at once, saving precious time.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// Your code here
return 0;
}
Fast I/O reduces the risk of “Time Limit Exceeded” errors. ios_base::sync_with_stdio(false) disconnects C++ streams from C streams, while cin.tie(NULL) ensures cin and cout don’t wait for each other.
Data Types and Variables in C++ Cheatsheet
Choosing the right data type prevents logic errors and integer overflows. Here’s a quick reference:
| Data Type | Keyword | Size (Bytes) | Range / Use Case |
| Integer | int | 4 | Whole numbers up to 10^9 |
| Long Integer | long long | 8 | Large numbers up to 10^18 |
| Character | char | 1 | Single letters or symbols |
| Floating Point | float | 4 | Decimals (7 digits precision) |
| Double Float | double | 8 | Decimals (15 digits precision) |
| Boolean | bool | 1 | True or False values |
Using this C++ cheatsheet PDF ensures you pick the right type every time.
Pointers and References in C++ Cheatsheet
Understanding memory management is vital.
- Reference (&): An existing variable.
- Pointer (*): Stores memory addresses of other variables.
- Address-of (&): Calculates memory location for a variable.
- Dereference (*): Retrieves data stored at memory location.
Pointers are essential in DSA problems, helping manage memory efficiently.
Conditional Statements in C++
- if – executes code if condition is true
- if-else – executes alternative if false
- else-if ladder – multiple conditions
- switch – choose among multiple values
- Ternary operator ? : – shorthand conditional
Loops in C++
- for – compact iteration
- while – repeats while condition is true
- do-while – executes at least once
STL Containers in C++ Cheatsheet
The Standard Template Library (STL) simplifies data handling.
Vectors
Dynamic arrays that grow or shrink:
vector<int> v;
v.push_back(5);
v.pop_back();
v.size();
Set and Map
- Set: Stores unique sorted elements.
- Map: Key-value pairs.
- Unordered versions: Faster O(1) average access (unordered_set, unordered_map).
Stack and Queue
- Stack: LIFO – push(), pop(), top().
- Queue: FIFO – push(), pop(), front().
Breaking STL into snippets keeps this C++ cheatsheet readable and easy to reference.
Math and String Functions in C++ Cheatsheet
The built-in functions make coding challenges easier:
- sqrt(x) – Square root.
- pow(x, y) – x to the power y.
- abs(x) – Absolute value.
- s.length() – String length.
- s.substr(pos, len) – Substring extraction.
These are handy for solving both numeric and text-based problems quickly.
Algorithms in C++ Cheatsheet
Prebuilt algorithms save time:
- Sorting: sort(v.begin(), v.end());
- Binary Search: binary_search(start, end, key);
- Reverse: reverse(v.begin(), v.end());
- Max/Min Element: *max_element(v.begin(), v.end());
Knowing these makes solving DSA problems faster.
Input/Output Examples C++
int x;
cin >> x; // input
cout << x; // output
File Handling in C++
#include <fstream>
ofstream fout(“file.txt”);
fout << “Hello”;
fout.close();
ifstream fin(“file.txt”);
string s;
fin >> s;
fin.close();
Common Mistakes in C++
Avoid errors during coding:
- Using = instead of == in conditional statements.
- Not using a semicolon ; at the end of each statement.
- Array index errors.
Using this C++ cheatsheet PDF will help you avoid these mistakes in your code.
Also Read:
FAQs
1. Why use #include bits/stdc++.h in a C++ cheatsheet?
It is a "super header" that loads every standard library at once. This saves you from having to remember specific headers like vector or algorithm during a timed exam.
2. Can this C++ cheatsheet help in practicing DSA?
This cheatsheet is a useful resource for DSA practice, particularly because it's tailored for C++. It covers the DSA section and provides details on the Standard Template Library. You'll find time-saving functions like sort() and binary_search() included as well.
3. What is the difference between an array and a vector?
The size of an array is fixed and cannot be altered at a later stage. However, a vector is a dynamic array that increases or decreases in size based on the information being added or deleted.
4. How do pointers work in C++?
Pointers in C++ are special variables that store the "address" of another variable's memory location in your computer.
5. Which STL container is best for unique elements?
Use a set if you need your elements to be unique and sorted. If you need them to be unique but don't need them to be sorted and need faster access, use an unordered_set.
