C++11C++14C++17Learn C++

Traversing sequences without writing explicit loops

The posts General Loop Statements in Modern C++ and Range-for-statement in modern C++ cover ways to write explicit loops. But explicit loops can be tedious to write and, what is more important, – harder to read, because the resulting code requires to spend the extra time by others in order to understand what is going on in the explicit loop. As alternative, the C++ standard library provides…
Read more
C++11C++14C++17Learn C++

Switch Statement in Modern C++

The C++ language provides the switch statement which can be used to replace the set of if statements (see If Statements in Modern C++). First of all, let’s define the enum type Traffic_light_color as follows: enum class Traffic_light_color { red, yellow, green…
C++11C++14C++17Learn C++Numerics

Introduction to Random Number Generation in Modern C++

Every implementation of the C++ Standard Library like one included to Embarcadero RAD Studio C++ compilers provides two ways for (pseudo-)random numbers generation: old-fashioned facilities from <cstdlib> and modern facilities provided by <random>. Facilities from <cstdlib> There are two very simple functions provided by the C random library: std::srand() and std::rand().
Read more
C++C++11C++14C++17Iterators

Insert Iterators Adapters in C++

Let’s slightly modify the example from The Move Iterator Adapter in C++ post: #include <algorithm> #include <list> #include <string> #include <vector> /// @warning BUG! PLEASE, DON'T USE! auto deep_copy_to_list(const std::vector<std::string>& src) { std::list<std::string> dst; // constructs the empty list copy(cbegin(src), cend(src)…
Read more
C++C++11C++14C++17IteratorsLearn C++

Categories of Iterators in C++

Conceptually, iterators are consists of five categories: input iterator is intended to traverse sequences in the forward direction and provides the read access to the pointed sequence element;output iterator is intended to traverse sequences in the forward direction and provides the write access to the pointed sequence element;forward iterator is intended to traverse sequences in the forward…
Read more