Within the previous articles about STL algorithms I introduced the main algorithms sorted by their use case. Within this article I want to give an overview about the STL operations. These operations can be used stand-alone and within STL algorithms. The combination of the existing algorithms and operations will allow to solve complex implementations of some standard use cases in an easy way.
Compare two elements
Algorithm | Description |
equal_to | Check whether the two elements are equal. |
not_equal_to | Check whether the two elements are not equal. |
greater | Check whether the first element is greater than the second one. |
less | Check whether the first element is smaller than the second one. |
greater_equal | Check whether the first element is greater or equal to the second one. |
less_equal | Check whether the first element is smaller or equal to the second one. |
max | Returns the larger one of the two elements. |
min | Returns the smaller one of the two elements. |
minmax | Returns a pair which contains the smaller element followed by the larger element. |
Within the examples the operations “greater” and “less” are used to sort a container. The third example makes use of “max” operator to merge two containers by taking the maximum value of each element pair.
void Print(const std::vector& data) { std::for_each(data.begin(), data.end(), [](int element){ std::cout << element << " "; }); std::cout << std::endl; } int _tmain(int argc, _TCHAR* argv[]) { std::vector data{ 5, 7, 9, 2, 4 }; // greater std::sort(data.begin(), data.end(), std::less()); Print(data); // output is '2 4 5 7 9' // less std::sort(data.begin(), data.end(), std::greater()); Print(data); // output is '9 7 5 4 2' // max std::vector data1{ 5, 7, 9, 2, 4 }; std::vector data2{ 3, 8, 1, 5, 8 }; std::vector target; std::transform(data1.begin(), data1.end(), data2.begin(), std::back_inserter(target), [](int a, int b) {return std::max(a, b); }); Print(target); // output is '5 8 9 5 8' return 0; }
Search elements in container
Algorithm | Description |
max_element | Returns an iterator to the largest element of the container. |
min_element | Returns an iterator to the smallest element of the container. |
minmax_element | Returns a pair which contains an iterator to the smallest element of the container followed by an iterator to the largest element of the container. |
int _tmain(int argc, _TCHAR* argv[]) { std::vector data{ 5, 7, 9, 2, 4 }; // max_element auto iterator = std::max_element(data.begin(), data.end()); if (iterator != data.end()) { std::cout << "max element " << *iterator << " found at index " << std::distance(data.begin(), iterator) << std::endl; // output is 'max element 9 found at index 2' } return 0; }
Calculations with one input
Algorithm | Description |
negate | Returns the negation of the element. |
clamp | Returns the given element with respect to a value range. If the given element is outside of the range the returned value will be equal to the lowest or highest boundary.
Example: “clamp(x,20,30)” means the returned value must be in range 20 to 30. If x is less than 20 “20” will be returned. If x is between 20 to 30 “x” will be returned. If x is larger than 30 “30” will be returned. |
void Print(const std::vector& data) { std::for_each(data.begin(), data.end(), [](int element){ std::cout << element << " "; }); std::cout << std::endl; } int _tmain(int argc, _TCHAR* argv[]) { std::vector data{ 5, -7, 9, -2, 4 }; // negate std::transform(data.begin(), data.end(), data.begin(), std::negate()); Print(data); // output is '-5 7 -9 2 -4' return 0; }
Calculations with two inputs
Algorithm | Description |
plus | Returns the sum of the two elements. |
minus | Returns the difference of the two elements. |
multiplies | Returns the product of the two elements. |
divides | Returns the quotient of the two elements. |
modulus | Returns the residual value of the division of first element by second element. |
logical_and | Returns the result of a logical-and calculation of the two elements. |
logical_or | Returns the result of a logical-or calculation of the two elements. |
logical_not | Returns the result of a logical-not calculation of the two elements. |
bit_and | Returns the result of a bitwise-and calculation of the two elements. |
bit_or | Returns the result of a bitwise-or calculation of the two elements. |
bit_xor | Returns the result of a bitwise-xor calculation of the two elements. |
bit_not | Returns the result of a bitwise-not calculation of the two elements. |
void Print(const std::vector& data) { std::for_each(data.begin(), data.end(), [](int element){ std::cout << element << " "; }); std::cout << std::endl; } int _tmain(int argc, _TCHAR* argv[]) { std::vector source1{ 5, 7, 9, 2, 4 }; std::vector source2{ 3, 1, 2, 5, 7 }; std::vector target; std::vector source3{ 1, 1, 0, 0 }; std::vector source4{ 1, 0, 1, 0 }; // sum std::transform(source1.begin(), source1.end(), source2.begin(), std::back_inserter(target), std::plus()); Print(target); // output is '8 8 11 7 11' // logical or target.clear(); std::transform(source3.begin(), source3.end(), source4.begin(), std::back_inserter(target), std::logical_or()); Print(target); // output is '1 1 1 0' // bitwise xor target.clear(); std::transform(source3.begin(), source3.end(), source4.begin(), std::back_inserter(target), std::bit_xor()); Print(target); // output is '0 1 1 0' return 0; }
Pingback: STL Algorithms | coders corner