The C++ STL Algorithm Library provides predefined functions for performing common operations such as searching, sorting, counting, comparing, and modifying data stored in containers. These algorithms are mainly defined in the <algorithm> and <numeric> header files.
- Reduces development time by providing efficient implementations of common operations.
- Works with different STL containers through iterators, improving code reusability.
Type of Algorithms
The algorithms in the C++ STL can be broadly classified into the following categories:
1. Searching Algorithms
Searching algorithms are used to locate elements or determine whether a particular value exists in a container. They help perform search operations efficiently without manually traversing the container.
Some commonly used searching algorithms are:
- find(): Searches for an element and returns its iterator if present.
- find_if(): Finds the first element satisfying a given condition.
- binary_search(): Checks if an element exists in a sorted container.
- lower_bound(): Returns an iterator to the first element not less than a given value.
- upper_bound(): Returns an iterator to the first element greater than a given value.
- find_end(): Finds the last occurrences of a subsequence in a range.
2. Sorting and Rearranging Algorithms
These algorithms change the order or arrangement of elements within a container.
- sort(): Arranges elements in a given range into ascending order by default.
- stable_sort(): Sorts elements while preserving the relative order of equal elements.
- partial_sort(): Sorts only the first N elements in order, leaving the rest unsorted.
- nth_element(): Rearranges elements so the nth position has the correct element as in sorted order, with smaller ones before it and larger ones after it (but unordered).
- is_sorted(): Checks whether a range is already sorted.
- is_sorted_until(): Returns an iterator to the first element where the sorted order breaks.
3. Manipulation Algorithms
Modification algorithms change, replace, copy, or rearrange elements in a container.
- copy(): Copies element from one range into another range, preserving their order.
- copy_if(): Copies only the elements that satisfy a given condition to another range.
- copy_n(): Copies exactly n elements from a source range to a destination range.
- copy_backward(): Copies elements in reverse order starting from the end.
- move(): Moves elements from one range to another.
- swap(): Exchanges the values of two elements.
- replace(): Replaces all occurrences of a specific value in a range with a new value.
- replace_if(): Replaces all elements that satisfy a given condition with a new value.
- remove():Moves unwanted elements to the end of the range and returns the new logical end.
- fill(): Assigns a specified value to every element in a given range.
- transform(): Applies a function to each element and stores the result.
- generate(): Assigns values to a range by repeatedly calling a given function or generator.
- shuffle(): Randomly rearranges the elements in a given range.
4. Counting & Comparing Algorithms
These algorithms are used to count elements or compare ranges of elements.
- count(): Returns the number of elements in a range that are equal to a specified value.
- count_if(): Returns the number of elements in a range that satisfy a given condition.
- equal(): Checks if two ranges have the same elements in the same order.
- mismatch(): Finds the first position where two ranges differ.
- lexicographical_compare(): Checks if one range is lexicographically less than another.
- is_permutation(): Checks whether two ranges contain the same elements in any order.
5. Numeric Algorithms (<numeric>)
The <numeric> header provides algorithms for mathematical and numerical operations.
- accumulate(): Computes the sum (or custom accumulation) of elements in a range.
- iota(): Fills a range with sequentially increasing values.
- partial_sum(): Computes cumulative sums of elements.
- inner_product(): Computes the inner (dot) product of two ranges.
Advantages of STL Algorithms
The STL Algorithm Library provides several advantages:
- Works with different container types through iterators.
- Offers optimized and efficient implementations.
- Improves code readability and reusability.
- Integrates seamlessly with STL containers.