C++ Basics Cheat Sheet

C++ Programming Basics Cheat Sheet

Download PDF: C++ Programming Basics Cheat Sheet.pdf

Basic Structure of a C++ Program

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Variables and Data Types

Variables
int number = 10;           // Integer
double pi = 3.14;          // Double
char letter = 'A';         // Character
std::string greeting = "Hello"; // String (requires <string> header)
bool isTrue = true;        // Boolean
Data Types
int i = 100;               // Integer
float f = 10.5f;           // Floating point
double d = 10.5;           // Double floating point
char c = 'A';              // Character
bool b = true;             // Boolean
std::string s = "Hello";   // String (requires <string> header)

Control Flow

If Statements
if (condition) {
    // code block
} else if (anotherCondition) {
    // another code block
} else {
    // another code block
}
Switch Statement
int day = 2;
switch (day) {
    case 1:
        std::cout << "Monday" << std::endl;
        break;
    case 2:
        std::cout << "Tuesday" << std::endl;
        break;
    // other cases
    default:
        std::cout << "Invalid day" << std::endl;
}
Loops
For Loop
for (int i = 0; i < 5; i++) {
    std::cout << i << std::endl;
}
While Loop
int i = 0;
while (i < 5) {
    std::cout << i << std::endl;
    i++;
}
Do-While Loop
int i = 0;
do {
    std::cout << i << std::endl;
    i++;
} while (i < 5);

String Operations

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello, World!";
    std::cout << s.length() << std::endl;        // Output: 13
    std::cout << s.substr(7, 5) << std::endl;    // Output: World
    std::cout << s.find("World") << std::endl;   // Output: 7

    std::string s2 = s.replace(7, 5, "C++");
    std::cout << s2 << std::endl;                // Output: Hello, C++!
    
    return 0;
}

File Operations

Reading from a File
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::ifstream file("filename.txt");
    std::string line;
    if (file.is_open()) {
        while (getline(file, line)) {
            std::cout << line << std::endl;
        }
        file.close();
    } else {
        std::cout << "Unable to open file";
    }
    return 0;
}
Writing to a File
#include <iostream>
#include <fstream>

int main() {
    std::ofstream file("filename.txt");
    if (file.is_open()) {
        file << "Hello, file!\n";
        file.close();
    } else {
        std::cout << "Unable to open file";
    }
    return 0;
}

Comments

Single-Line Comments
// This is a single-line comment
int x = 10;  // This is an inline comment
Multi-Line Comments
/*
This is a multi-line comment
spanning multiple lines.
*/
int y = 20;

Exception Handling

#include <iostream>
#include <stdexcept>

int main() {
    try {
        int result = 10 / 0;
    } catch (const std::exception &e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    } catch (...) {
        std::cerr << "Unknown exception!" << std::endl;
    }

    return 0;
}

Common Libraries

Importing Libraries
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <utility>

Basic Operators

Arithmetic Operators
+   // Addition
-   // Subtraction
*   // Multiplication
/   // Division
%   // Modulus
++  // Increment
--  // Decrement
Comparison Operators
==  // Equal to
!=  // Not equal to
>   // Greater than
<   // Less than
>=  // Greater than or equal to
<=  // Less than or equal to
Logical Operators
&&  // Logical AND
||  // Logical OR
!   // Logical NOT

Functions

Defining and Calling Functions
#include <iostream>

// Function definition
int add(int a, int b) {
    return a + b;
}

int main() {
    // Function call
    int result = add(5, 3);
    std::cout << result << std::endl;  // Output: 8
    return 0;
}
Function Declaration (Prototype)
#include <iostream>

// Function declaration
int add(int, int);

int main() {
    int result = add(5, 3);
    std::cout << result << std::endl;  // Output: 8
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;
}
Function Overloading
#include <iostream>

// Overloaded functions
int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

int main() {
    std::cout << add(5, 3) << std::endl;       // Output: 8
    std::cout << add(5.5, 3.3) << std::endl;   // Output: 8.8
    return 0;
}

Arrays

Defining and Using Arrays
int numbers[5] = {1, 2, 3, 4, 5};

// Access elements
std::cout << numbers[0] << std::endl;  // Output: 1

// Loop through array
for (int i = 0; i < 5; i++) {
    std::cout << numbers[i] << std::endl;
}

// Using range-based for loop (C++11 and later)
for (int number : numbers) {
    std::cout << number << std::endl;
}

Data Structures

Vector
#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};

    // Access elements
    std::cout << vec[0] << std::endl;  // Output: 1

    // Add elements
    vec.push_back(6);

    // Loop through vector
    for (int i = 0; i < vec.size(); i++) {
        std::cout << vec[i] << std::endl;
    }

    // Using range-based for loop (C++11 and later)
    for (int val : vec) {
        std::cout << val << std::endl;
    }

    return 0;
}
Map
#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> ageMap;
    ageMap["Alice"] = 30;
    ageMap["Bob"] = 25;

    // Access elements
    std::cout << "Alice's age: " << ageMap["Alice"] << std::endl;

    // Loop through map
    for (const auto &pair : ageMap) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    return 0;
}
Set
#include <iostream>
#include <set>

int main() {
    std::set<int> mySet = {1, 2, 3, 4, 5};

    // Add element
    mySet.insert(6);

    // Check if element exists
    if (mySet.find(3) != mySet.end()) {
        std::cout << "Element found" << std::endl;
    } else {
        std::cout << "Element not found" << std::endl;
    }

    // Loop through set
    for (int val : mySet) {
        std::cout << val << std::endl;
    }

    return 0;
}
List
#include <iostream>
#include <list>

int main() {
    std::list<int> myList = {1, 2, 3, 4, 5};

    // Add elements
    myList.push_back(6);
    myList.push_front(0);

    // Loop through list
    for (int val : myList) {
        std::cout << val << std::endl;
    }

    // Remove elements
    myList.pop_front();
    myList.pop_back();

    return 0;
}