Royce  Reinger

Royce Reinger


C-Plus-Plus: The Algorithms - C++ # {#mainpage}

The Algorithms - C++ # {#mainpage}


This repository is a collection of open-source implementation of a variety of algorithms implemented in C++ and licensed under MIT License. These algorithms span a variety of topics from computer science, mathematics and statistics, data science, machine learning, engineering, etc.. The implementations and the associated documentation are meant to provide a learning resource for educators and students. Hence, one may find more than one implementation for the same objective but using a different algorithm strategies and optimizations.


  • The repository provides implementations of various algorithms in one of the most fundamental general purpose languages - C++.
  • Well documented source code with detailed explanations provide a valuable resource for educators and students alike.
  • Each source code is atomic using STL classes and no external libraries are required for their compilation and execution. Thus, the fundamentals of the algorithms can be studied in much depth.
  • Source codes are compiled and tested for every commit on the latest versions of three major operating systems viz., Windows, MacOS and Ubuntu (Linux) using MSVC 16 2019, AppleClang 11.0 and GNU 7.5.0 respectively.
  • Strict adherence to C++11 standard ensures portability of code to embedded systems as well like ESP32, ARM Cortex, etc. with little to no changes.
  • Self-checks within programs ensure correct implementations with confidence.
  • Modular implementations and OpenSource licensing enable the functions to be utilized conveniently in other applications.


Online Documentation is generated from the repository source codes directly. The documentation contains all resources including source code snippets, details on execution of the programs, diagrammatic representation of program flow, and links to external resources where necessary. The documentation also introduces interactive source code with links to documentation for C++ STL library functions used. Click on Files menu to see the list of all the files documented with the code.

Documentation of Algorithms in C++ by The Algorithms Contributors is licensed under CC BY-SA 4.0


As a community developed and maintained repository, we welcome new un-plagiarized quality contributions. Please read our Contribution Guidelines.

Download Details:

Author: TheAlgorithms
Source Code: 
License: MIT license

#search #computerscience #machinelearning #algorithm 

What is GEEK

Buddha Community

C-Plus-Plus: The Algorithms - C++ # {#mainpage}
Tamale  Moses

Tamale Moses


How to Run C/C++ in Sublime Text?

C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.

In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.

During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. VimSublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.

I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen file related functions in C/C++.

#cpp #c #c-programming #sublimetext #c++ #c/c++

Dicey Issues in C/C++

If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends. The following displays some of the issues:

  1. Using multiple variables in the print function
  2. Comparing Signed integer with unsigned integer
  3. Putting a semicolon at the end of the loop statement
  4. C preprocessor doesn’t need a semicolon
  5. Size of the string matters
  6. Macros and equations aren’t good friends
  7. Never compare Floating data type with double data type
  8. Arrays have a boundary
  9. Character constants are different from string literals
  10. Difference between single(=) and double(==) equal signs.

The below code generates no error since a print function can take any number of inputs but creates a mismatch with the variables. The print function is used to display characters, strings, integers, float, octal, and hexadecimal values onto the output screen. The format specifier is used to display the value of a variable.

  1. %d indicates Integer Format Specifier
  2. %f indicates Float Format Specifier
  3. %c indicates Character Format Specifier
  4. %s indicates String Format Specifier
  5. %u indicates Unsigned Integer Format Specifier
  6. %ld indicates Long Int Format Specifier

Image for post

A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a non-negative integer in the range [0 to 4294967295]. The signed integer is represented in twos-complement notation. In the below code the signed integer will be converted to the maximum unsigned integer then compared with the unsigned integer.

Image for post

#problems-with-c #dicey-issues-in-c #c-programming #c++ #c #cplusplus

Cayla  Erdman

Cayla Erdman


C++ Classes and Objects

This article will use a CheckingAccount class as a running example.

Writing classes

When writing classes one has to consider the behaviors and data members (data) represented by the class. A class is like a blueprint and objects are like buildings constructed from the blueprint.

Class definitions

Here is the first cut of the CheckingAccount class:


#ifndef CHECKING_H
#define CHECKING_H
class CheckingAccount
        CheckingAccount(double balance);
        void deposit(double amount);
        bool withdraw(double amount);
        double getBalance() const;
        double mBalance;

checkingaccount.cpp: Note that the :: operator is the scope resolution operator.

CheckingAccount::CheckingAccount(double balance) //constructor
    mBalance = balance;
void CheckingAccount::deposit(double amount)
    mBalance += amount;
bool CheckingAccount::withdraw(double amount)
    if((mBalance - amount) > 0)
        mBalance -= amount;
        return true;
    return false;
double getBalance() const
    return mBalance;
//Destructor not implemented as there is no dynamic memory allocated.

Access control

  • public: Any code can call a public member function or access a public data member.
  • protected: Only the class and its derived classes can access the member function or data member
  • private : Only the class can access the member function or data member.

In-Class Member Initializers

One can initialize member variables directly in the class definition like so:

class CheckingAccount
private : 
    double mBalance = 0.0;

this pointer

Each member function of a class has an implicit pointer parameter called this which points to the current object. It can be used to disambiguate parameters from data members. It can also be used to pass a reference to the current object to a function that takes a reference or const reference to the object.

Objects on the stack

C++ allows objects to be allocated on the stack as well as the heap. Example:

CheckingAccount chkAccount(1000.00);
cout << "Balance is " << chkAccount.getBalance() << endl;

Objects on the heap/free store

Objects can be allocated on the heap using either raw pointers or one of the smart pointers. Example:

auto myChkAccount = std::make_unique<CheckingAccount>(10000.00); //smart pointer
cout << myChkAccount->getBalance() << endl;

CheckingAccount * chkaccount = new CheckingAccount(10000.00); //prefer smart pointer over this
cout << chkaccount->getBalance() << endl;
delete chkaccount;

#c-plus-plus-language #coding #c-plus-plus-training #programming-languages #c#

Modern C++: Strings and string_view

C-style strings

C style strings should be avoided except when interfacing with C libraries. C string library functions provide no bounds checking and memory allocation support. They are represented as an array of characters. Last character of the string is the null character \0, so that code that uses the string knows where it ends. The space needed for a string is always one more than the number of readable characters.

String Literals

Strings written with quotes around them are string literals. They are stored in a read-only part of memory. Because they are stored in readonly sections, attempting to modify string literals is undefined behavior. Example:

char *str = "world";
str[0] = 'y'; //undefined behavior

If the code respected the standard and assigned the string literal to const char*, the compiler will catch attempts to modify string literals:

const char * str = "world";
str[0] = 'k'; //compiler will flag this as error.

To mutate strings, assign them to character arrays. In this example, the compiler creates an array large enough to hold the string and copies the string to the array. The literal is not put in read only memory.

char str[] = "hello world";
str[0] = 'y';

Raw string literals

C++ offers raw string literals which are literals that span multiple lines of code, and don’t require escaping of embedded double quotes, and don’t treat escape sequences with any special logic. Example:

const char* str = R"(Brave New World)";
const char* str = R"(Line1: Brave 
Line2: New World)";

To use ) or ( in raw string literals, use unique delimiter sequences like the following:

const char* str = R"-(Embedded ) parens)-";

#c-plus-plus-language #c-plus-plus-training #programming-languages #programming #software-development #c++

Pointers and Dynamic Memory in C++

Pointers and Dynamic Memory

Dynamic memory is useful when you don’t know the memory requirements at compile time.

Stack and the Heap

Memory is divided into the stack and the heap or free store. Stack memory is automatically managed, whereas heap memory is not. Activation Records are blocks of memory on the stack that are used to implement functions. For example, when main calls foofoo’s activation record is at the top of the stack. Once foo returns, the activation record for foo is marked as deallocated, and another function call can make use of that space. Any parameters passed from main to foo are copied into foo’s activation record. Below is an illustration for the activation record from Wikipedia:

Image for post

Memory allocated on the heap lives indepedently of the function calls. One has to make sure to deallocate memory allocated on the heap. Some languages such as C## and Python have garbage collection (GC) for this purpose but C++ does not. However, “smart pointers” help with automatic deallocation of memory. To explicitly allocate memory on the heap:

int* pointerToInt;

pointerToInt is currently an uninitialized pointer. Working with unitialized pointers will likely crash the program. One should always initialize pointers. They can be set to null (using nullptr), and checked against null. nullptr converts to false when used in a Boolean expression. Example:

    //execute code that handles null pointer case

The new operator is used to allocate memory.

pointerToInt = new int;

To access the newly allocated value, one has to dereference the pointer, or follow the pointer’s address to memory. For example, setting the value of the pointer above can be done as follows:

*pointerToInt = 42;

#programming-languages #c-plus-plus-training #c-plus-plus-language #c++