Skip to content
Shop

CommunityJoin Our PatreonDonate

Sponsored Ads

Sponsored Ads

Libraries

<iostream>: Input and Output

cpp
#include <iostream>
using namespace std;

int main() {
    string name;
    int age;

    cout << "Enter your name: ";
    cin >> name;

    cout << "Enter your age: ";
    cin >> age;

    cout << "Hello, " << name << "! You are " << age << " years old." << endl;

    return 0;
}

<vector>: Dynamic Arrays

cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> numbers = {10, 20, 30};

    numbers.push_back(40); // Add an element
    numbers.pop_back();    // Remove the last element

    cout << "Vector elements: ";
    for (int num : numbers) cout << num << " ";

    return 0;
}

<map>: Associative Containers

cpp
#include <iostream>
#include <map>
using namespace std;

int main() {
    map<string, int> scores;

    scores["Alice"] = 90;
    scores["Bob"] = 85;

    cout << "Alice's score: " << scores["Alice"] << endl;

    cout << "All scores:\n";
    for (auto& pair : scores) {
        cout << pair.first << ": " << pair.second << endl;
    }

    return 0;
}

<algorithm>: Standard Algorithms

cpp
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

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

    // Sort the vector
    sort(nums.begin(), nums.end());

    // Find an element
    auto it = find(nums.begin(), nums.end(), 4);
    if (it != nums.end()) {
        cout << "Found 4 at index " << distance(nums.begin(), it) << endl;
    }

    // Print sorted vector
    cout << "Sorted Vector: ";
    for (int num : nums) cout << num << " ";

    return 0;
}

<cmath>: Math Operations

cpp
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double x = 9.0;

    cout << "Square root of " << x << ": " << sqrt(x) << endl;
    cout << "Power of 2^3: " << pow(2, 3) << endl;
    cout << "Sine of 45 degrees: " << sin(45 * M_PI / 180) << endl;

    return 0;
}

<fstream>: File Input/Output

cpp
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    // Writing to a file
    ofstream outFile("example.txt");
    outFile << "Hello, File!" << endl;
    outFile.close();

    // Reading from a file
    ifstream inFile("example.txt");
    string content;
    while (getline(inFile, content)) {
        cout << content << endl;
    }
    inFile.close();

    return 0;
}

<thread>: Multithreading

cpp
#include <iostream>
#include <thread>
using namespace std;

void printNumbers() {
    for (int i = 1; i <= 5; ++i) {
        cout << "Number: " << i << endl;
    }
}

int main() {
    thread t1(printNumbers);  // Start a thread
    t1.join();               // Wait for the thread to complete

    cout << "Main thread ends here." << endl;
    return 0;
}

<chrono>: Timing and Delays

cpp
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;

int main() {
    using namespace chrono;

    auto start = high_resolution_clock::now();

    this_thread::sleep_for(seconds(2)); // Delay for 2 seconds

    auto end = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(end - start).count();

    cout << "Elapsed time: " << duration << " ms" << endl;

    return 0;
}

<random>: Random Numbers

cpp
#include <iostream>
#include <random>
using namespace std;

int main() {
    random_device rd; // Seed
    mt19937 gen(rd()); // Mersenne Twister generator
    uniform_int_distribution<> dist(1, 100); // Range: 1 to 100

    cout << "Random number: " << dist(gen) << endl;

    return 0;
}