Getting Started
5 snippetsYour first steps with C++ basics
Hello World
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
} Comments
// Single line comment
/* Multi-line
comment */ User Input
std::string name;
std::cout << "Name: ";
std::cin >> name; Type Casting
static_cast<int>(3.14) // C++ style
(int)3.14 // C style
std::stoi("42") // string to int Compile & Run
g++ -o main main.cpp // Compile
./main // Run Variables & Data Types
9 snippetsStore and manage data in C++
Int
int age = 25; Double
double price = 19.99; String
std::string name = "John"; Bool
bool isActive = true; Auto
auto count = 10; Const
const double PI = 3.14159; Array
int nums[] = {1, 2, 3, 4, 5}; Vector
std::vector<int> vec = {1, 2, 3}; Map
std::map<std::string, int> ages; Operators
7 snippetsSymbols for calculations and comparisons
Arithmetic
+ - * / // Add, Sub, Mul, Div
% // Modulus
++ -- // Increment/Decrement Comparison
== != // Equal, Not equal
> < // Greater, Less than
>= <= // Greater/Less or equal
<=> // Spaceship (C++20) Logical
&& // AND
|| // OR
! // NOT Assignment
= += -= *= /= %=
&= |= ^= <<= >>= Bitwise
& // AND
| // OR
^ // XOR
~ // NOT
<< // Left shift
>> // Right shift Pointer/Reference
&x // Address of x
*p // Value at pointer p
-> // Member access via pointer Memory
new // Allocate
delete // Deallocate
sizeof(int) // Size in bytes Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Loops
9 snippetsRepeat code efficiently with iterations
For
for (int i = 0; i < 5; i++) {
std::cout << i;
} Range-based For
for (const auto& item : vec) {
std::cout << item;
} While
while (count < 10) {
count++;
} Do While
do {
count++;
} while (count < 10); Break
for (int i = 0; i < 10; i++) {
if (i == 5) break;
} Continue
for (int i = 0; i < 10; i++) {
if (i == 5) continue;
std::cout << i;
} Iterator Loop
for (auto it = vec.begin(); it != vec.end(); ++it) {
std::cout << *it;
} For Each (Algorithm)
#include <algorithm>
std::for_each(vec.begin(), vec.end(),
[](int n) { std::cout << n; }); Indexed Range
for (size_t i = 0; i < vec.size(); i++) {
std::cout << i << ": " << vec[i];
} Conditionals
5 snippetsDirect program execution with conditions
If/Else
if (x > 0) {
std::cout << "positive";
} else if (x < 0) {
std::cout << "negative";
} else {
std::cout << "zero";
} Ternary
int max = (a > b) ? a : b; Switch
switch (day) {
case 1:
std::cout << "Monday";
break;
default:
std::cout << "Other";
} If with Init (C++17)
if (auto it = map.find(key); it != map.end()) {
std::cout << it->second;
} Constexpr If (C++17)
if constexpr (std::is_integral_v<T>) {
// Compile-time branch
} Functions
5 snippetsReusable blocks of code and lambdas
Function
std::string greet(std::string name) {
return "Hello, " + name + "!";
} Reference
void increment(int& x) {
x++;
} Const Ref
void print(const std::string& s) {
std::cout << s;
} Default Param
void log(std::string msg, int level = 0) {} Lambda
auto add = [](int a, int b) { return a + b; }; Pointers & Memory
5 snippetsPointer
int* ptr = &x; Dereference
int value = *ptr; New/Delete
int* p = new int(5);
delete p; Smart Pointer
std::unique_ptr<int> ptr = std::make_unique<int>(5); Shared Pointer
std::shared_ptr<int> ptr = std::make_shared<int>(5); Classes & OOP
3 snippetsClass
class Dog {
private:
std::string name;
public:
Dog(std::string n) : name(n) {}
std::string bark() {
return name + " says woof!";
}
}; Inheritance
class Puppy : public Dog {
public:
Puppy(std::string n) : Dog(n) {}
}; Virtual
virtual void speak() = 0; // Pure virtual STL Algorithms
4 snippetsSort
std::sort(vec.begin(), vec.end()); Find
auto it = std::find(vec.begin(), vec.end(), 5); Transform
std::transform(vec.begin(), vec.end(), vec.begin(),
[](int x) { return x * 2; }); Accumulate
int sum = std::accumulate(vec.begin(), vec.end(), 0); Error Handling
2 snippetsTry/Catch
try {
throw std::runtime_error("Error");
} catch (const std::exception& e) {
std::cerr << e.what();
} Throw
throw std::invalid_argument("Invalid input");