1667579359
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.
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.
Author: TheAlgorithms
Source Code: https://github.com/TheAlgorithms/C-Plus-Plus
License: MIT license
#search #computerscience #machinelearning #algorithm
1624240146
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. Vim, Sublime 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++
1597937354
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:
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.
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.
#problems-with-c #dicey-issues-in-c #c-programming #c++ #c #cplusplus
1599424680
This article will use a CheckingAccount
class as a running example.
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.
Here is the first cut of the CheckingAccount
class:
checkingaccount.h
:
#ifndef CHECKING_H
#define CHECKING_H
class CheckingAccount
{
public:
CheckingAccount(double balance);
~CheckingAccount();
void deposit(double amount);
bool withdraw(double amount);
double getBalance() const;
private:
double mBalance;
}
#endif
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.
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 memberprivate
: Only the class can access the member function or data member.One can initialize member variables directly in the class definition like so:
class CheckingAccount
//....
private :
double mBalance = 0.0;
this
pointerEach 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.
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 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#
1599572340
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.
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';
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++
1599565080
Dynamic memory is useful when you don’t know the memory requirements at compile time.
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 foo
, foo
’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:
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:
if(!pointerToInt)
{
//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++