Data Structures and Algorithms
C++-latest data structures and algorithms using only standard libraries. This answers many of the challenges in HackerRank, LeetCode and some in Codility. > 2005 Google Test cases and counting.
Dependencies
Permuted Congruential Generator-64 (PCG64)
Windows
Google Test
Build the latest GTest and GMock libraries from source
Download GoogleTest source code from https://github.com/google/googletest/releases
To build dynamically-link library with address sanitizer, open Developer Command Prompt in Visual Studio and run the following commands in sequence:
i. rmdir /s /q build
ii.
cmake -S . -B build -G "Visual Studio 18 2026" -A x64 -DBUILD_SHARED_LIBS=ON -Dgtest_force_shared_crt=ON -DCMAKE_DEBUG_POSTFIX=_debug -Dgtest_build_tests=OFF -Dgmock_build_tests=OFF -DCMAKE_CXX_FLAGS="/fsanitize=address,undefined,leak" -DCMAKE_C_FLAGS="/fsanitize=address,undefined,leak"
iii.cmake --build build --config Debug --target gtest
iv. cmake --build build --config Release --target gtest
v. Add build/bin/Debug and build/bin/Release to %PATH% environment variable.
Note: gtest_main.dll is not required for the application to link against the library.
The gtest_main target simply provides a tiny boilerplate main function that initializes GoogleTest and runs the tests automatically.
Because we skipped building it to bypass the MSVC C2491 compiler error, use main.cpp to provide own entry point.
Create a Google Test project, manage nuget packages and uninstall the package Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn
Set the property page
C/C++ > General > Additional Include Directories: adds the googletest/include and googlemock/include paths.
Linker > General > Additional Library Directories: point to the path of the compiled .lib file (e.g. googletest\build\lib\Debug OR googletest\build\lib\Release).
Linker > Input > Additional Dependencies: adds gtest.lib.
C/C++ > Code Generation > Run Library: match the compilation configuration of the Googletest libraries (e.g. MTd for Debug mode)
Then the Google Test project can be built successfully.
Address Sanitizer
Google Test
- Add the path of
clang_rt.asan_dynamic-x86_64.dll to User and System environment variables. At the time of this writing and for Visual Studio 2026, the path is C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.52.37025\bin\Hostx64\x64.
Note that the path changes every time Visual Studio is upgraded, especially the 14.52.37025 string in the path.
Ubuntu
Visual Studio Code with the following extensions:
To convert Visual Studio solution .slnx and included .vcxproj to CMakeLists.txt:
Install the following packages on Ubuntu:
Optional: Download googletest from https://github.com/google/googletest/releases and extract to /usr/src/googletest
To check the libgtest-dev installed:
$ dpkg -s libgtest-dev | grep Version
Build
The build configurations are defined in CMakePresets.json:
| Configure preset |
Build / test presets |
Sanitizers |
Binaries |
linux-asan |
linux-asan-debug, linux-asan-release |
On |
./Debug, ./Release |
linux-release |
linux-release |
Off (LTO) |
build/linux-release/bin/Release |
linux-valgrind |
linux-valgrind |
Off |
build/linux-valgrind/bin/Debug |
windows-msvc |
windows-msvc-debug, windows-msvc-release |
n/a |
.\Debug, .\Release |
Put machine-specific presets in CMakeUserPresets.json (git-ignored).
Command line
$ cmake --list-presets
$ cmake --preset linux-asan
$ cmake --build --preset linux-asan-debug
$ ctest --preset linux-asan-debug
Visual Studio Code
- Press
CTRL + SHFT + P + CMAKE: Select Configure Preset and choose a preset. For example, "Linux GCC + sanitizers". This replaces CMAKE: Select Variant.
- Press
CTRL + SHFT + P + CMAKE: Select Build Preset to choose the target build type. For example, "Debug + sanitizers".
- Press
CTRL + SHFT + P + CMAKE: Configure - This needs to be done after rm -rf build/ folder or the Presets: clean all task.
- Press
CTRL + SHFT + B to run the default build task, CMake: build all. It builds the all target straight away, without a list to choose from, using the configure and build presets selected in the CMake pane (see "Project Status" > "Configure" and "Build").
- Press
CTRL + SHFT + P + Tasks: Run Task to run any other task in .vscode/tasks.json:
| Task |
What it does |
CMake: build Console / Core / Tests |
Builds one target with the presets selected in the CMake pane |
Presets: build linux-asan-debug |
Configures and builds Debug with sanitizers, whatever is selected in the CMake pane |
Presets: build linux-release |
Configures and builds Release without sanitizers (LTO), whatever is selected in the CMake pane |
Presets: clean all |
Deletes build/, Debug/ and Release/ so that the next build starts from scratch |
- The launch configurations in
.vscode/launch.json debug the binaries in ./Debug, i.e. the linux-asan-debug build preset.
Debug / Run
CTRL + K + S to check keyboard shortcuts. Filter for cmake.
| Key |
What it runs |
Under gdb? |
| F5 |
The selected launch.json entry |
Yes |
| SHFT + F5 |
"CMake: Debug" on the pane's debug target |
Yes |
| CTRL + SHFT + F5 |
"CMake: Run Without Debugging" on the pane's launch target |
No |
| CTRL + F5 |
"Start Without Debugging" on the pane's launch target |
No |
- The entries in
launch.json only run with F5, using whichever one is selected in the "Run and Debug" dropdown.
Continuous Integration: