Features of the Standard Algorithms
Get introduced to the different features of the standard algorithms library.
To get a better understanding of the standard algorithms, it's good to know a bit about the features and common patterns used by all algorithms in the <algorithm>
header. As already stated, the algorithms under the std
and std::ranges
namespaces have a lot in common. We will start here with the general principles that are true for both the std algorithms and the constrained algorithms under std::range
. Then, in the next section, we will move on to discuss the features that are specific to the constrained algorithms found under std::ranges
.
Algorithms do not change the size of the container
Functions from <algorithm>
can only modify the elements in a specified range; elements are never added or deleted from the underlying container. Therefore, these functions never alter the size of the container that they operate on.
For example, std::remove()
or std::unique()
do not actually remove elements from a container (despite their names). Rather, it moves the elements that should be kept to the front of the container and then returns a sentinel that defines the new end of the valid range of elements:
Get hands-on with 1400+ tech skills courses.