Tag Archives: c++

C++ || How To Get Distinct & Unique Values In An Array/Vector/Container & Remove Duplicates Using C++

The following is a module with functions which demonstrates how to get distinct and unique values in an array/vector/container and remove duplicates using C++.

The function demonstrated on this page is a template, so it should work on containers of any type.

The function also does not require a sort, it preserves the original order of the vector, uses the equality operator (operator==), and allows for a custom predicate comparison function to determine whether the arguments are equal.


1. Distinct – String Array

The example below demonstrates the use of ‘Utils::distinct‘ to get the distinct elements from an array.


2. Distinct – Object Vector

The example below demonstrates the use of ‘Utils::distinct‘ to get the distinct elements from an object vector.

The object in this example overloads the equality operator to determine whether arguments are equal.


3. Distinct – Object Vector Predicate

The example below demonstrates the use of ‘Utils::distinct‘ to get the distinct elements from an object vector.

In this example, a predicate is used to determine whether arguments are equal.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Create A Simple Predictive Spell Checker Using C++

The following is a module with functions which demonstrates how to create a simple predictive spell checker using C++.

The module demonstrated on this page is an implementation based on the Norvig Spelling Corrector.


1. Overview

In its simplest form, this process works by first telling the spelling corrector the valid words in our language. This is called “training” the corrector. Once the corrector is trained to know the valid words in our language, it tries to choose the word that is the most likely spelling correction.

This process is done by choosing the candidate with the highest probability to show up in our language. For example, should “lates” be corrected to “late” or “latest” or “lattes” or …? This choice is determined by the word which has the highest probability to appear in our language, according to the training text we provide.

Traditionally, spell checkers look for four possible errors: a wrong letter (“wird”), also knows as alteration. An inserted letter (“woprd”), a deleted letter (“wrd”), or a pair of adjacent transposed letters (“wrod”). This process is used when generating probable spelling correction candidates.

The training text used in the example on this page can be downloaded here. This file consists of 80891 distinct words, which is used to train the spelling corrector.


2. Spell Check – Basic Usage

The example below demonstrates the use of the ‘SpellChecker‘ class to load the training file and correct the spelling of a word.


3. Spell Check – Unit Tests

The example below demonstrates the use of the ‘SpellChecker‘ class to load the training file and correct the spelling of a word.

In this example, words are added to a set to determine if the most likely spelling correction is returned by the spelling corrector. The first word in the set is the word to test against (some are spelled correctly, some incorrectly). The second word in the set is the expected result that should be returned by the spelling corrector.

If no spelling suggestion is found, an empty string is returned by the spelling corrector. This is true for one of the words in the test cases below. Even though the word is spelled correctly, it is not defined in our training text, and no correction can be found.


4. SpellChecker Class

The following is the SpellChecker Class. Include this in your project to start using!

The following is the header file ‘SpellChecker.h‘.

The following is the implementation file ‘SpellChecker.cpp‘.


5. More Examples

Below are more examples demonstrating the use of the ‘SpellChecker‘ class. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Remember to include the training file.

C++ || How To Execute & Get Regex Match Results Using C++

The following is a module with functions which demonstrates how to execute and get regex match results using C++.

The function demonstrated on this page uses regex to search a specified input string for all occurrences of a specified regular expression. The function returns a collection of std::smatch objects found by the search.


1. Get Regex Matches

The example below demonstrates the use of ‘Utils::getRegexMatches‘ to search an input string for all occurrences of a regular expression.


2. Get Regex Matches – Sub Match

The example below demonstrates the use of ‘Utils::getRegexMatches‘ to search an input string for all occurrences of a regular expression.

In this example, sub matches are expected and displayed.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Trim & Remove The Leading & Trailing Whitespace From A String Using C++

The following is a module with functions which demonstrates how to trim and remove the leading and trailing whitespace from a string using C++.


1. Trim

The example below demonstrates the use of ‘Utils::trim‘ to remove all the leading and trailing whitespace characters from a string.


2. Trim Start

The example below demonstrates the use of ‘Utils::trimStart‘ to remove all leading whitespace characters from a string.


3. Trim End

The example below demonstrates the use of ‘Utils::trimEnd‘ to remove all trailing whitespace characters from a string.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Check If A String Starts & Ends With A Certain String Using C++

The following is a module with functions which demonstrates how to determine if a string starts and ends with a certain substring using C++.


1. Starts With

The example below demonstrates the use of ‘Utils::startsWith‘ to determine whether the beginning of a string instance matches a specified string value.


2. Ends With

The example below demonstrates the use of ‘Utils::endsWith‘ to determine whether the end of a string instance matches a specified string value.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Read An Entire File Into A String & Write A String To A File Using C++

The following is a module with functions which demonstrates how to write a string to a file and read an entire file to a string using C++.

The functions demonstrated on this page uses fstream to read and write data to and from a file.


1. Read All Text

The example below demonstrates the use of ‘Utils::readAllText‘ to open a text file, read all the text in the file into a string, and then close the file.


2. Write All Text

The example below demonstrates the use of ‘Utils::writeAllText‘ to create a new file, write a specified string to the file, and then close the file. If the target file already exists, it is overwritten.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Get A List Of Files At A Given Path Directory Using C++

The following is a module with functions which demonstrates how to get a list of files at a given directory path using C++.

The function demonstrated on this page returns a list of std::filesystem::directory_entry, which contains information about the files in the given directory.


1. Get Files In Directory

The example below demonstrates the use of ‘Utils::getFilesInDirectory‘ to get a list of files at a given path directory.

The optional function parameter lets you specify the search option. Set the search option to True to limit the search to just the current directory. Set the search option to False to expand the search to the current directory and all subdirectories when searching for files.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Make Map & Unordered Map Keys Case Insensitive Using C++

The following is a module with functions which demonstrates how to make map and unordered_map keys case insensitive using C++.


1. Case Insensitive – Map

The example below demonstrates how to make a map with a string key case insensitive.

To do this, the comparison function needs to be overridden.


2. Case Insensitive – Unordered Map

The example below demonstrates how to make an unordered map with a string key case insensitive.

To do this, the comparison and the hash function needs to be overridden.


3. Full Examples

Below is a full example demonstrating the concepts on this page.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Search For & Find A Specific Element In A Vector Using C++

The following is a module with functions which demonstrates how to search for and find a specific element in a vector using C++.

The function demonstrated on this page is a template, so it should work on vectors of any type. It also uses a predicate to determine the item to select.


1. Find

The example below demonstrates the use of ‘Utils::find‘ to find the first element in a sequence of values based on a predicate.

The predicate determines the item to find.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Filter & Select Items In An Array/Vector/Container & Get The Results Using C++

The following is a module with functions which demonstrates how to filter and select items in an array/vector/container, and get the results using C++.

The function demonstrated on this page is a template, so it should work on containers of any type. It also uses a predicate to determine the items to select.


1. Filter – Integer Array

The example below demonstrates the use of ‘Utils::filter‘ to filter an integer array based on a predicate and return its results.

The predicate determines the items to filter and select.


2. Filter – String Vector

The example below demonstrates the use of ‘Utils::filter‘ to filter a sequence of values based on a predicate and return its results.

The predicate determines the items to filter and select.


3. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


4. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Check If An Item Exists In A Vector And Get Its Position Index Using C++

The following is a module with functions which demonstrates how to find the position index of an item in a vector, and make sure the item exists using C++.

The functions demonstrated on this page are wrappers for std::find_if function, and are templates, so they should work on vectors/containers of any type.


1. Contains

The example below demonstrates the use of ‘Utils::contains‘ to determine whether an element is contained in the collection.


2. Index Of

The example below demonstrates the use of ‘Utils::indexOf‘ to get the zero based index of the first occurrence of a search value in the collection.


3. Index Of – Predicate

The example below demonstrates the use of ‘Utils::indexOf‘ to get the zero based index of the first occurrence of a search value in the collection.

In this example, a predicate is used to determine the item to search for. This allows you to customize how an item should be searched.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Merge & Concatenate Two Vectors Using C++

The following is a module with functions which demonstrates how to merge, concatenate and append two vectors using C++.

The function demonstrated on this page is a template, so it should work on vectors of any type.


1. Add Range

The example below demonstrates the use of ‘Utils::addRange‘ add elements to the end of the collection.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Replace All Occurrences Of A String With Another String Using C++

The following is a module with functions which demonstrates how to replace all occurrences of a substring with another substring using C++.


1. Replace All

The example below demonstrates the use of ‘Utils::replaceAll‘ to replace all occurrences of an ‘oldValue’ string with a ‘newValue’ string.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || How To Check If A String Is Empty Or Only Contains Whitespace Using C++

The following is a module with functions which demonstrates how to determine whether a string is empty, or consists only of white-space characters using C++.


1. Is Null Or Whitespace

The example below demonstrates the use of ‘Utils::isNullOrWhitespace‘ to determine whether a string is empty, or consists only of white-space characters.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.