All Cheat Sheets
C++
C++ Cheat Sheet
Basics
Hello WorldBasic C++ program#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}Data typesPrimitive typesint x = 10;
float f = 3.14f;
double d = 3.14159;
char c = 'A';
bool b = true;Input/OutputConsole I/Oint age;
cout << "Enter age: ";
cin >> age;
cout << "Age: " << age << endl;ConstantsImmutable valuesconst int MAX = 100;
const double PI = 3.14159;Control Flow
If-elseConditional executionif (x > 0) {
cout << "positive";
} else {
cout << "not positive";
}For loopIterationfor (int i = 0; i < 10; i++) {
cout << i << endl;
}Range-based forModern iteration (C++11)vector<int> nums = {1, 2, 3};
for (int n : nums) {
cout << n << endl;
}OOP
ClassBlueprint for objectsclass Car {
private:
string model;
public:
Car(string m) : model(m) {}
void drive() {
cout << model << " is driving\n";
}
};InheritanceDerived classesclass ElectricCar : public Car {
public:
ElectricCar(string m) : Car(m) {}
};Virtual functionPolymorphismclass Shape {
public:
virtual double area() = 0;
};STL
VectorDynamic array#include <vector>
vector<int> nums = {1, 2, 3};
nums.push_back(4);
nums.size();MapKey-value pairs#include <map>
map<string, int> ages;
ages["Mishu"] = 25;StringString class#include <string>
string s = "hello";
s.length();
s.substr(0, 2);SortSorting algorithm#include <algorithm>
sort(nums.begin(), nums.end());