What is the algorithms library in C++?
The algorithm library has several built-in functions that provide a variety of functionalities (e.g., sorting, searching, counting, modifying, and non-modifying sequence operations). All these functions are designed to be used on ranges of elements.
A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or instance of some of the STL containers.
Some of the most important functions of the library are described below:
Sorting functions
sort
The sort function sorts the elements in the range [first,last) into ascending order.
The elements are compared using the < operator.
Equivalent elements are not guaranteed to keep their original, relative order.
The parameters of this function are:
- Iterators to the initial and final position of the sequence to be sorted.
- An optional binary function that accepts two elements in the range as arguments, and returns a value convertible to
bool.
#include <algorithm>#include <vector>int main () {int myints[] = {43, 23, 65, 2, 5, 24, 49, 33};vector<int> myvector (myints, myints+8);// using default comparison (operator <):sort (myvector.begin(), myvector.begin()+8); //(12 32 45 71 26 80 53 33)cout<<" Sorted vector: ";for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)cout << ' ' << *it;cout << '\n';}
is_sorted
The is_sorted function returns true if the range [first,last) is sorted into ascending order.
The parameters of this function are:
- Iterators to the initial and final position of the sequence to be sorted.
- An optional binary function that accepts two elements in the range as arguments, and returns a value convertible to
bool.
Merge functions
merge
The merge function combines the elements in the sorted ranges, [first1, last1) and [first2,last2), into a new range (beginning at result) that has all its elements sorted.
The parameters of this function are:
- Iterators to the initial and final positions of both the sequences that are to be merged.
- Output iterator to the initial position of the range where the result of the merge is to be stored.
- An optional binary function that accepts two elements in the range as arguments, and returns a value convertible to
bool.
#include <algorithm> // std::merge, std::sortint main () {int first[] = {30, 53, 12, 82, 23};int second[] = {50, 40, 30, 20, 10};std::vector<int> v(10);std::sort (first,first+5);std::sort (second,second+5);std::merge (first,first+5,second,second+5,v.begin());std::cout << "Merged vector is: ";for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;}
includes
The function returns true if the sorted range [first1,last1) contains all the elements in the sorted range [first2,last2).
The parameters of this function are:
- Iterators to the initial and final positions of the first sorted sequence.
- Iterators to the initial and final positions of the second sorted sequence.
- Output iterator to the initial position of the range where the result of the merge is to be stored.
- An optional binary function that accepts two elements in the range as arguments, and returns a value convertible to
bool.
#include <iostream> // std::cout#include <algorithm> // std::includes, std::sortbool myfunction (int i, int j) { return i<j; }int main () {int container[] = {5,10,15,20,25,30,35,40,45,50};int continent[] = {40,30,20,10};std::sort (container,container+10);std::sort (continent,continent+4);// using myfunction as the comparator function:if ( std::includes(container,container+10,continent,continent+4, myfunction) )std::cout << "The first sequence contains the second sequence\n";return 0;}
Min/max functions
min_element
Returns an iterator pointing to the element with the smallest value in the range [first, last). The parameters for this function are:
- Iterators to the initial and final positions of the sequence being compared.
- An optional binary function that accepts two elements in the range as arguments, and returns a value convertible to
bool. The value returned indicates whether or not the element passed as the first argument is considered less than the second.
It returns the iterator to the minimum element in the range.
max_element
Returns an iterator pointing to the element with the largest value in the range [first, last). The parameters for this function are:
- Iterators to the initial and final positions of the sequence being compared.
- An optional binary function that accepts two elements in the range as arguments, and returns a value convertible to
bool. The value returned indicates whether or not the element passed as the first argument is considered less than the second.
It returns the iterator to the maximum element in the range.
#include <iostream>#include <algorithm>int main () {int myints[] = {43, 23, 65, 2, 5, 24, 49, 33};std::cout << "The smallest element is " << *std::min_element(myints,myints+8) << '\n';std::cout << "The largest element is " << *std::max_element(myints,myints+8) << '\n';return 0;}
The complete list of all the functions that are part of this library, and their usage, can be found on the official documentation page.
Free Resources