Category: Example

https://cdn3d.iconscout.com/3d/premium/thumb/business-idea-3d-illustration-download-in-png-blend-fbx-gltf-file-formats–planning-creative-innovative-strategy-pack-illustrations-3010206.png?f=webp

  • Function Templates with Multiple Parameters

    This example shows how to define a function template that works with multiple parameters.

    cppCopy code#include <iostream>
    
    template <typename T1, typename T2>
    void displayPair(T1 first, T2 second) {
    
    std::cout &lt;&lt; "First: " &lt;&lt; first &lt;&lt; ", Second: " &lt;&lt; second &lt;&lt; std::endl;
    } int main() {
    displayPair(42, "Hello");          // Output: First: 42, Second: Hello
    displayPair(3.14, 100);            // Output: First: 3.14, Second: 100
    return 0;
    }
  • Range-Based For Loop

    This example illustrates how to use the range-based for loop introduced in C++11.

    cppCopy code#include <iostream>
    #include <vector>
    
    int main() {
    
    std::vector&lt;int&gt; numbers = {10, 20, 30, 40, 50};
    // Using a range-based for loop
    for (const auto&amp; num : numbers) {
        std::cout &lt;&lt; num &lt;&lt; " "; // Output: 10 20 30 40 50
    }
    std::cout &lt;&lt; std::endl;
    return 0;
    }
  • Using std::thread with Member Functions

    This example demonstrates how to create threads that run member functions of a class.

    cppCopy code#include <iostream>
    #include <thread>
    
    class Counter {
    public:
    
    void countUp(int limit) {
        for (int i = 1; i &lt;= limit; ++i) {
            std::cout &lt;&lt; i &lt;&lt; " ";
        }
        std::cout &lt;&lt; std::endl;
    }
    }; int main() {
    Counter counter;
    std::thread t(&amp;Counter::countUp, &amp;counter, 5); // Pass the class instance and arguments
    t.join(); // Wait for the thread to finish
    return 0;
    }
  • Smart Pointers with std::shared_ptr

    This example shows how to use std::shared_ptr for shared ownership of dynamically allocated objects.

    cppCopy code#include <iostream>
    #include <memory>
    
    class Resource {
    public:
    
    Resource() { std::cout &lt;&lt; "Resource acquired." &lt;&lt; std::endl; }
    ~Resource() { std::cout &lt;&lt; "Resource released." &lt;&lt; std::endl; }
    }; int main() {
    std::shared_ptr&lt;Resource&gt; res1 = std::make_shared&lt;Resource&gt;();
    {
        std::shared_ptr&lt;Resource&gt; res2 = res1; // Shared ownership
        std::cout &lt;&lt; "Inside the block." &lt;&lt; std::endl;
    } // res2 goes out of scope, but res1 still holds the resource
    std::cout &lt;&lt; "Exiting main." &lt;&lt; std::endl;
    return 0;
    }
  • Using std::map

    This example demonstrates how to use a map to store key-value pairs.

    cppCopy code#include <iostream>
    #include <map>
    #include <string>
    
    int main() {
    
    std::map&lt;std::string, int&gt; ageMap;
    
    // Inserting key-value pairs
    ageMap&#91;"Alice"] = 30;
    ageMap&#91;"Bob"] = 25;
    ageMap&#91;"Charlie"] = 35;
    // Accessing and displaying elements
    for (const auto&amp; pair : ageMap) {
        std::cout &lt;&lt; pair.first &lt;&lt; " is " &lt;&lt; pair.second &lt;&lt; " years old." &lt;&lt; std::endl;
    }
    
    return 0;
    }
  • Using STL Containers

    This example shows how to use the Standard Template Library (STL) with a std::vector.

    cppCopy code#include <iostream>
    #include <vector>
    
    int main() {
    
    std::vector&lt;int&gt; nums = {1, 2, 3, 4, 5};
    // Adding an element
    nums.push_back(6);
    // Iterating through the vector
    for (const auto&amp; num : nums) {
        std::cout &lt;&lt; num &lt;&lt; " "; // Output: 1 2 3 4 5 6
    }
    std::cout &lt;&lt; std::endl;
    return 0;
    }
  • Dynamic Memory Allocation

    This example demonstrates how to allocate and deallocate memory dynamically.

    cppCopy code#include <iostream>
    
    int main() {
    
    int* array = new int&#91;5]; // Allocate an array of 5 integers
    for (int i = 0; i &lt; 5; ++i) {
        array&#91;i] = i * 10; // Initialize the array
    }
    for (int i = 0; i &lt; 5; ++i) {
        std::cout &lt;&lt; array&#91;i] &lt;&lt; " "; // Output: 0 10 20 30 40
    }
    std::cout &lt;&lt; std::endl;
    delete&#91;] array; // Deallocate memory
    return 0;
    }
  • Constexpr

    An example that shows how to use constexpr for compile-time evaluation.

    cppCopy code#include <iostream>
    
    constexpr int factorial(int n) {
    
    return n &lt;= 1 ? 1 : n * factorial(n - 1);
    } int main() {
    std::cout &lt;&lt; "Factorial of 5: " &lt;&lt; factorial(5) &lt;&lt; std::endl; // Output: 120
    return 0;
    }
  • Lambda Expressions

    This example illustrates how to use lambda expressions for concise function definitions.

    cppCopy code#include <iostream>
    #include <vector>
    #include <algorithm>
    
    int main() {
    
    std::vector&lt;int&gt; numbers = {1, 2, 3, 4, 5};
    std::for_each(numbers.begin(), numbers.end(), &#91;](int n) {
        std::cout &lt;&lt; n * n &lt;&lt; " "; // Output: 1 4 9 16 25
    });
    std::cout &lt;&lt; std::endl;
    return 0;
    }
  • Namespaces

    This example shows how to use namespaces to organize code and avoid naming conflicts.

    cppCopy code#include <iostream>
    
    namespace Math {
    
    int add(int a, int b) {
        return a + b;
    }
    } namespace StringUtils {
    void printHello() {
        std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl;
    }
    } int main() {
    std::cout &lt;&lt; "Sum: " &lt;&lt; Math::add(3, 4) &lt;&lt; std::endl; // Output: Sum: 7
    StringUtils::printHello(); // Output: Hello, World!
    return 0;
    }