Jay Abhani
Senior Web Development Instructor at almaBetter
Comprehensive C++ cheat sheet covering syntax, STL, data structures, strings, and competitive programming essentials. Ideal for beginners and interview prep.
C++ is one of the most widely used programming languages in the world. It’s known for its performance, flexibility, and strong support for object-oriented programming. Whether you're a computer science student, a software engineer, or an aspiring competitive programmer, this C++ cheat sheet will serve as a compact reference to the language's most essential features, functions, and concepts.
This guide covers everything from syntax to data structures, providing both newcomers and seasoned coders with a reliable and comprehensive resource.
C++ is a general-purpose programming language created as an extension of the C language. It combines low-level memory manipulation capabilities with high-level abstractions, making it suitable for system programming, game development, competitive programming, and large-scale software systems.
A good beginner C++ cheat sheet should start with the building blocks of the language basic syntax, control flow, and standard input/output before diving into more complex areas like the Standard Template Library (STL), data structures, and algorithms.
A typical C++ program looks like this:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
This structure is the core of any C++ program, and understanding it is the first step in any C++ syntax cheat sheet.
Key Points:
#include
is used for including standard libraries.using namespace std;
helps avoid writing std::
before standard functions.main()
is the entry point.cout
and cin
handle output and input respectively.int age = 30;
float price = 19.99;
double pi = 3.14159;
char grade = 'A';
bool isOpen = true;
C++ has strong static typing, meaning variables must be declared with a type. For most programming tasks, these basic types form the foundation.
C++ supports arithmetic (+
, -
, *
, /
, %
), relational (==
, !=
, <
, >
), logical (&&
, ||
, !
), assignment (=
, +=
, -=
, etc.), and bitwise operators (&
, |
, ^
, ~
, <<
, >>
).
These operators are critical when writing conditions, expressions, and control flows.
if (x > 0) {
cout << "Positive";
} else if (x < 0) {
cout << "Negative";
} else {
cout << "Zero";
}
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
while (x > 0) {
x--;
}
do {
x++;
} while (x < 10);
Knowing how to control program execution is key, and no C++ cheat sheet is complete without covering these.
int add(int a, int b) {
return a + b;
}
int main() {
cout << add(5, 3);
}
Functions help break down complex problems into manageable chunks. Parameters can be passed by value or reference, making C++ versatile in handling memory and performance.
Arrays are used for storing collections of data.
int arr[5] = {1, 2, 3, 4, 5};
cout << arr[2]; // Output: 3
C++ strings come in two types: C-style (char[]
) and std::string
.
string name = "Alice";
cout << name.length(); // 5
Some common string operations:
string s = "Hello";
s.append(" World");
cout << s.substr(0, 5); // Hello
cout << s.find("World"); // Position of "World"
Pointers allow direct manipulation of memory.
int a = 10;
int* p = &a;
cout << *p; // Output: 10
References:
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int* ptr = new int;
*ptr = 100;
delete ptr;
// For arrays:
int* arr = new int[5];
delete[] arr;
C++ supports key OOP concepts: classes, objects, inheritance, polymorphism, encapsulation, and abstraction.
class Car {
public:
string brand;
void honk() {
cout << "Beep!";
}
};
class ElectricCar : public Car {
public:
int batteryLife;
};
Common STL Containers:
vector
– dynamic arrayset
– sorted unique elementsmap
– key-value pairsunordered_map
– hash tablestack
, queue
, priority_queue
#include <vector>
vector<int> v = {1, 2, 3};
v.push_back(4);
sort(v.begin(), v.end());
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); ++it) {
cout << *it << " ";
}
// With C++11 and above:
for (int x : v) {
cout << x;
}
sort(arr, arr+n);
reverse(v.begin(), v.end());
max_element(arr, arr+n);
count(v.begin(), v.end(), 3);
This C++ data structures cheat shec++ string functions cheat sheet covers widely used types:
Each of these structures supports key operations with efficient time complexity, making them essential for solving algorithmic problems.
try {
throw 20;
} catch (int e) {
cout << "Exception: " << e;
}
namespace MySpace {
int x = 100;
}
cout << MySpace::x;
#include <fstream>
ofstream out("file.txt");
out << "Hello File!";
out.close();
ifstream in("file.txt");
string content;
in >> content;
cout << content;
C++ is the most widely used language in competitive programming due to its execution speed and rich STL support. A good C++ DSA cheat sheet will always emphasize templates, macros, and fast input/output techniques like:
ios::sync_with_stdio(false);
cin.tie(NULL);
Some must-know tricks include:
lower_bound
and upper_bound
std::binary_search
sort()
priority_queue
as min-heapsFor those preparing for coding competitions, a specialized C++ competitive programming can be a game-changer.
template <typename T>
T maxVal(T a, T b) {
return (a > b) ? a : b;
}
auto add = [](int a, int b) { return a + b; };
cout << add(2, 3); // Output: 5
This C++ cheat sheet brings together the essentials of C++ programming into a single reference. From syntax and structure to STL and algorithms, we’ve covered a broad spectrum of features that every programmer should know.
Whether you’re brushing up for interviews, preparing for coding competitions, or diving into system-level programming, this guide — inspired by the needs of a C++ DSA cheat sheet — serves as a reliable companion. You now have a solid foundation to tackle real-world problems, practice efficient coding, and build high-performance applications.
For deeper exploration, consider combining this with other focused references like a C++ string cheat sheet or C++ array cheat sheet, depending on your goals. No matter where you are in your learning journey, mastering these core elements is key to becoming a proficient C++ developer.
More Cheat Sheets and Top Picks