Standard Template Library – Algorithm
Algorithms are predefined function to perform a specific functionality. In STL, the built-in header files are <algorithm> and <numeric>.
Algorithms can be classified into 4 types based on its
functionality.
- 1. Sorting algorithms
- 2. Searching algorithms.
- 3. Manipulation algorithm
- 4. Counting and comparing algorithm
Let us discuss about
the algorithm in each category.
1.
Sorting algorithms:
This is the algorithm which arranges the
element in particular order. The functions are given below.
“sort()” : it
arranges the elements in particular order.
“nth_element()” :
it rearranges the element based on the ‘n’ value.
“is_sorted()”: it
checks the given array is sorted or not.
“is_sorted_until()”:
it returns the first element where the sorted order breaks.
“stable_sort()”:
it sorts the elements and also preserves the relative order.
“partial_sort()”:it
sorts the first n elements in particular order.
2.Searching algorithms:
It searches the particular element in
the given array. it has lot of built-in functions as follows.
‘binary_search()’: sorts based on binary search algorithm.
‘lower_bound()’: it gives the lower bound value.
‘upper_bound()’: it gives the upper bound value.
‘find()’: searches an element and gives the element is
present or not.
‘find_if()’ : it finds the first data which satisfies the
condition.
‘find_end()’: it finds the last element of the data structures.
‘count()”: it gives you the number of elements.
‘count_if()’: it gives you the count within the range.
3.Manipulation Algorithms
This
algorithm handles the operations on the data elements. The functions are given
below.
‘fill()’ : it fills the same data in all elements.
‘copy()’ : it copies the element in the range.
‘copy_if()’ : it copies the element if the condition met.
‘copy_n()’: it copies the n elements in the range.
‘copy_backward()’: it copies the data in the backward.
‘move()’: move a set of values to particular range.
‘swap()’ :it exchanges the value.
‘replace()’: replace a specific value.
‘replace_if()’ :replace a value when it met a condition.
‘remove()” : it deletes the data.
‘transform()’: it makes the data range with another value.
‘generate()’: it generates the value.
‘shuffle()’: it rearranges the data randomly.
4. Counting and comparing algorithm
If you want to find the number of
elements and want to compare the data, these are the functions you need to
know.
‘count’: it returns the number of elements.
‘count_if’: it returns the number of elements in particular range.
Comparing algorithms:
‘mismatch()’: it
finds the position from which the range differs.
‘equal()’ : it checks for two ranges for order and its
element.
‘lexicographical_compare()’: The comparison is based on the
dictionary order.
‘is_permutation() : it checks for permutation order in the
elements.
These are the various types of algorithm functions in
Standard Template Library. Hope, this blog post is useful to you.
Comments
Post a Comment