C++ Code Snippets

Below are some useful C++ code snippets for use in everyday projects.

Here is an example of how to generate a random number between two values, in this case 1 and 59, then display it.

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{

    // initialise random seed.
    srand((unsigned) time(0));

    // Minimum and maximum values for random number.
    int min_val = 1;
    int max_val = 59;

    // Generate random number.
    int random_number = rand() % (max_val - min_val + 1) + min_val;

    // Display the number in the console/terminal.
    cout << random_number << endl;

    return 0;

}

Here, a random password is generated from a specified set of characters. The length of the password is also randomly selected from a given minimum and maximum length.

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
{

    // Possible characters in password.
    string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string numbers = "0123456789";
    string nonAlpha = "{]+-[*=@:)}$^%;(_!&#?>/|.";

    // Combine possible password characters into a single string.
    string charSet = alpha + numbers + nonAlpha;

    // Minimum and maximum password length.
    int minLength = 20;
    int maxLength = 30;

    // initialise random seed.
    srand((unsigned) time(0));

    // Select a random password length between the minimum and maximum.
    int length = rand() % (maxLength - minLength + 1) + minLength;

    // Variable for new password.
    string result = "";

    // Construct new password.
    for (int i = 1; i <= length; ++i)
    {

        // Add a randomly selected character to the new password.
        result += charSet[rand() % ((charSet.length()-1) + 1)];

    }

    // Display the password.
    cout << result << endl;

    return 0;

}

Below is an example of how to access various parts of a filesystem path. It should be noted that the 'filesystem' library became a part of the C++ standard in C++17. Prior to this, filesystem functionality was available in the 'boost' library.

#include <filesystem>
#include <iostream>

using namespace std;

// Create filesystem namespace alias.
namespace fs = std::filesystem;

int main()
{

    // Path to a file.
    auto path = fs::path{ "C:\\Demo\\Subfolder\\Test.txt" };

    // Display elements of the path.
    cout << "Root: " << path.root_name() << endl;
    cout << "Root directory: " << path.root_directory() << endl;
    cout << "Root path: " << path.root_path() << endl;
    cout << "Relative path: " << path.relative_path() << endl;
    cout << "Parent path: " << path.parent_path() << endl;
    cout << "Filename: " << path.filename() << endl;
    cout << "Stem: " << path.stem() << endl;
    cout << "Extension: " << path.extension() << endl;

    return 0;

}

The output from the above can be seen below.

Root: "C:"
Root directory: "\\"
Root path: "C:\\"
Relative path: "Demo\\Subfolder\\Test.txt"
Parent path: "C:\\Demo\\Subfolder"
Filename: "Test.txt"
Stem: "Test"
Extension: ".txt"