C++ cover

C++ File Extensions

As a C++ developer, you would see .cpp for C++ source files and .h for header files. However, you may have seen file extensions like .cxx, .cc or .hxx. What are these file extensions less commonly? Why are they used and not others? History Before C++, there was the C language. C used the extensions .c for C source files and .h for C header files. Later, when C++ came about the file extensions .c and .h were still being used for C++. However, C++ code became incompatible with C code, thus using the same file extension would cause confusion between C++ and C languages. There was a need at the time to differentiate C++ code from C code. ...

October 11, 2024
C++ cover

C++: How to Initialize a Vector

A std::vector (or vector if you using namespace std;) is a C++ data structure. vector is a dynamic array that provides fast lookups. vector can be included by #include <vector>, is part of the Standard Template Library (STL), and is in “standard” scope std::. A vector can be initialized in many different ways. This is not an exhaustive list but what is useful and more common. What is initialization Initialization refers to the process of setting the value of a variable for the first time at the time of construction. Regarding, vector we can set an initial values (elements). ...

September 28, 2024
C++ cover

C++: Three Dimensional Vector

During the creation of a 3-dimensional vector in C++ I was wondering what to call the third dimension, if the first two were called rows and columns. 2D Vector Initialization To begin, a 2D vector would be initialized like the following: int rows = 3; int columns = 5; int initVal = 0; // 2 dimensional vector vector<vector<int>> vec2D(rows, vector<int>(columns, initVal)); All fine and dandy! Let’s look at three dimensions… 3D Vector Initialization Initializing a 3D vector gets more complicated: ...

September 28, 2024
C++ cover

C++ Version History

Origin C++ was created by Bjarne Stroustrup. Dr. Stroustrup created C++ as a direct descendant of C language (specifically, descendant of Classic C and C89). C++ is not a descendant of C99 (therefore they are siblings). 1 C++ supports data abstraction, object-oriented programming (OOP), and generic programming. C++ can be considered a better language than C because C++ supports more programming styles, while being a superset of C. Some programmers write in C++ but only use C functionality (e.g., C99). ...

May 30, 2023
Generic cover

Cleaning Up Your Visual Studio 2019 Build with a Script

Clean the Visual Studio 2019 x64 Release or Debug build directory using Clean-Build.bat batch script. Place in the x64 directory and run the bat. Great if you need to archive your project before sending and don’t want to send a bunch of extra files. Tested with Visual Studio 2019 and 2022. The script can be expanded to remove other kinds of files. Files Removed File type Usage *.ipdb Incremental Link-Time Code Generation - Program database (PDB) file *.iobj Incremental Link-Time Code Generation - Object file *.pdb Program database (PDB) *.idb Intermediate debug file / state file used for minimal rebuild and incremental compilation *.ilk Intermediary linker files *.exp DLL exports *.obj Object file - compiled code that the linker uses *.tlog Used for incremental builds - information about all inputs / outputs *.FileListAbsolute.txt List of files built in current build *.recipe Used in Guidance Automation Toolkit Notes Important to use .\ before paths, otherwise it will execute from the drive’s root directory If there is more than one condition that needs to be used an if statement then must use nested if statements can use defined to check if a variable is defined or not defined if not defined. Clean-Build.bat @echo off setlocal enableextensions setlocal EnableDelayedExpansion set ExecutableName=%1% set BuildType=%2% if not defined BuildType ( echo Usage: Clean-Build.bat ExecutableName BuildType exit /b 1 ) if not "%BuildType%" == "Debug" ( if not "%BuildType%" == "Release" ( echo Usage: Clean-Build.bat ExecutableName BuildType echo Build must be Release or Debug exit /b 1 ) ) if not defined ExecutableName ( echo Usage: Clean-Build.bat ExecutableName BuildType exit /b 1 ) echo Cleaning: %ExecutableName% echo Build type: %BuildType% :: Release builds if "%BuildType%" == "Release" ( del .\%BuildType%\*.iobj del .\%BuildType%\*.ipdb ) :: Debug builds if "%BuildType%" == "Debug" ( del .\%BuildType%\*.ilk del .\%BuildType%\*.idb ) :: Both builds del .\%BuildType%\*.tlog del .\%BuildType%\*.obj del .\%BuildType%\*.recipe del .\%BuildType%\*.pdb del .\%BuildType%\*.log del .\%BuildType%\*.FileListAbsolute.txt rmdir /S /Q .\%BuildType%\%ExecutableName%.tlog echo Done

May 29, 2023
C++ cover

C++ Convert String to Int

Previously discussed Convert Char to Int in C++, in which we converted a single character to an integer. In this article we will build upon that idea by converting multiple characters to an integer. C++ Standard Library If want to use the built-in functions, the C++ standard has a couple built-in functions for converting strings to an integer. Namely there is atoi and std::stoi. atoi atoi is part of “C Standard General Utilities Library in the header <cstdlib>. ...

May 23, 2023
C++ cover

C++ Convert Char to Int

C/C++’s char built-in type is a 1-byte type. char can hold a value [-128, 127]. The name char can be confusing because a char can range from printable characters to non-printable characters. It is easier to think of char as a 1-byte signed integer. Convert Character char to Integer int - Modern C++ Since C++11, a new C++ expressions have been added to do compile time casting. Compile-time casting is safer as errors can be checked during compile-time over run-time. Specifically, static_cast was added which is perfect usage for converting character to integer. ...

May 20, 2023
C++ cover

C++ Substring

C++’s substring function substr returns a newly constructed string (copy of a portion of the original string) with the parameters of substr. More importantly, C++’s substring is a source of confusion. I wanted to discuss how the substring works and what are the common pitfalls. What does Substr do? substr copies a substring of the original string. A substring is a contiguous sequence of characters within a string. For example, nate is a substring of donate. A substring can also contain the entire original string. ...

May 20, 2023
Generic cover

Algorithms: Fibonacci Sequence

The Fibonacci sequence is a sequence in which each number is the sum of the two previous ones, starting with 0 and 1 as the base cases. A Fibonacci number is a number in the Fibonacci sequence. The golden ratio can be computed from the ratio between two Fibonacci numbers in consecutive sequence, as the sequence approaches infinity. Here we will talk about how to compute the nth number in the Fibonacci sequence. ...

April 25, 2023