C++ Algorithm
C++ Algorithm Functions
Last Updated : 14 Jan 2026
In C++, the standard template library (STL) provides a powerful collection of underlying functions under the <algorithm> header. These functions help in a wide range of operations on sequences, such as arrays, vectors, lists, and other containers. Algorithm functions are general-purpose and operate on iterators, which makes them reusable across several container types.
Types of Algorithm
There are mainly two types of algorithms. These are as follows:
- Non-mutating Algorithms
- Mutating Algorithms
Now, we will discuss these algorithms one by one.
Non-Mutating Algorithms
In C++, non-mutating algorithms operate on a sequence of elements without changing their values. Their major use is to execute queries, comparisons, and inspections across a variety of elements. These algorithms work well when the purpose is to retrieve information rather than change the contents of a container. Common examples include find(), which looks for a given value, and count(), which counts the number of times a particular element appears.
These functions are efficient, ensure data integrity, and are critical tools for analyzing or verifying elements in STL containers, such as vectors, lists, and arrays.
Mutating Algorithms
In C++, mutating algorithms are those algorithms that update or rearrange the elements in a range. These algorithms alter the original instead of creating a new container. There are several mutating algorithms, such as sort(), reverse(), rotate(), partition(), remove(), etc. These algorithms are commonly utilized for efficient in-place data transformations.
Member Functions of C++ Algorithm
Below is the list of all member functions of the map:
Standard Non-Modifying Sequence Algorithms
The given table shows several standard non-modifying sequence operations that are used in C++ Algorithms.
| Function | Description |
|---|---|
| all_of | This function is used to tests a condition to all the elements of the range. |
| any_of | This function is used to tests a condition to some or any of the elements of the range |
| none_of | This function is used to checks if none of the elements follow the condition or not. |
| for_each | The function applies an operation to all the elements of the range. |
| find | The function finds a value in the range. |
| find_if | The function finds for an element in the range. |
| find_if_not | The function finds an element in the range but in the opposite way as the above one. |
| find_end | The function is used to return the last element of the range. |
| find_first_of | The function finds for the element that satisfies a condition and occurs at the first. |
| adjacent_find | The function makes a search for finding the equal and adjacent elements in a range. |
| count | The function returns the count of a value in the range. |
| count_if | The function returns the count of values that satisfy a condition. |
| mismatch | The function returns the value in sequence which is the first mismatch. |
| equal | The function is used to check if the two ranges have all elements equal. |
| is_permutation | The function checks whether the range in reference is a permutation of some other range. |
| search | The function searches for the subsequence in a range. |
| search_n | The function searches the range for the occurrence of an element. |
C++ Non-Modifying Sequence Operations Example
Let us take an example to illustrate the non-modifying sequence operations in C++.
Example
Output:
All elements are even? false
Any element is odd? true
No negative elements? true
Elements: 2 4 6 8 10 3 4 6 8 10
First occurrence of 6 found at index: 2
First element > 8 is: 10
First non-even element: 3
Last occurrence of {4, 6} starts at index: 6
First matching element from {3,7,9} is: 3
First pair of adjacent equal elements: 3 and 3
Count of 4s in data: 2
Count of elements > 5: 6
First mismatch: 3 vs 0
Are d1 and d2 equal? false
Is b a permutation of a? true
Subsequence {6,8} found at index: 2
Modifying Sequence Operations
The given table shows several modifying sequence operations that are used in C++ Algorithm.
| Function | Description |
|---|---|
| copy | The function copies the range of elements. |
| copy_n | The function copies n elements of the range |
| copy_if | The function copies the elements of the range if a certain condition is fulfilled. |
| copy_backward | The function copies the elements in a backward order |
| move | The function moves the ranges of elements. |
| move_backward | The function moves the range of elements in the backward order |
| swap | The function swaps the value of two objects. |
| swap_ranges | The function swaps the value of two ranges. |
| iter_swap | The function swaps the values of two iterators under reference. |
| transform | The function transforms all the values in a range. |
| replace | The function replaces the values in the range with a specific value. |
| replace_if | The function replaces the value of the range if a certain condition is fulfilled. |
| replace_copy | The function copies the range of values by replacing with an element. |
| replace_copy_if | The function copies the range of values by replacing with an element if a certain condition is fulfilled. |
| fill | The function fills the values in the range with a value. |
| fill_n | The function fills the values in the sequence. |
| generate | The function is used for the generation of values of the range. |
| generate_n | The function is used for the generation of values of the sequence. |
| remove | The function removes the values from the range. |
| remove_if | The function removes the values of the range if a condition is fulfilled. |
| remove_copy | The function copies the values of the range by removing them. |
| remove_copy_if | The function copies the values of the range by removing them if a condition is fulfilled. |
| unique | The function identifies the unique element of the range. |
| unique_copy | The function copies the unique elements of the range. |
| reverse | The function reverses the range. |
| reverse_copy | The function copies the range by reversing values. |
| rotate | The function rotates the elements of the range in left direction. |
| rotate_copy | The function copies the elements of the range which is rotated left. |
| random_shuffle | The function shuffles the range randomly. |
| shuffle | The function shuffles the range randomly with the help of a generator. |
C++ Modifying Sequence Operations Example
Let us take an example to demonstrate the modifying sequence operations in C++.
Example
Output:
Copied vector: 1 2 3 4 5 6 7 8 9 10 Even elements (copy_if): 2 4 6 8 10 Transformed (doubled) vector: 2 4 6 8 10 12 14 16 18 20 Replaced 5 with 99: 1 2 3 4 99 6 7 8 9 10 Removed 3 from vector: 1 2 4 5 6 7 8 9 10 After unique(): 1 2 3 4 Reversed vector: 10 9 8 7 6 5 4 3 2 1 Rotated vector (left by 3): 4 5 6 7 8 9 10 1 2 3 After swap_ranges(), swapped1: 2 4 6 8 10 12 14 16 18 20 After swap_ranges(), swapped2: 1 2 3 4 5 6 7 8 9 10 Shuffled vector: 6 9 1 7 4 3 5 8 2 10
Partitions
The given table shows several partition operations that are used in C++ Algorithm.
| Function | Description |
|---|---|
| is_partitioned | The function is used to deduce whether the range is partitioned or not. |
| partition | The function is used to partition the range. |
| stable_partition | The function partitions the range in two stable halves. |
| partition_copy | The function copies the range after partition. |
| partition_point | The function returns the partition point for a range. |
C++ Partition Operations Example
Let us take an example to demonstrate the partition operations in C++.
Example
Output:
Is the data partitioned (evens first)? false After partition (evens first): 8 2 6 4 5 3 7 1 After stable_partition (evens first, order preserved): 2 4 6 8 1 3 5 7 Partitioned copy - evens: 8 2 6 4 Partitioned copy - odds: 5 3 7 1 Partition point index: 4
Sorting
The given table shows several sorting operations that are used in the C++ Algorithm.
| Function | Description |
|---|---|
| sort | The function sorts all the elements in a range. |
| stable_sort | The function sorts the elements in the range, maintaining the relative equivalent order. |
| partial_sort | The function partially sorts the elements of the range. |
| partial_sort_copy | The function copies the elements of the range after sorting it. |
| is_sorted | The function checks whether the range is sorted or not. |
| is_sorted_until | The function checks till which element a range is sorted. |
| nth_element | The functions sorts the elements in the range. |
C++ Sorting Operations Example
Let us take an example to demonstrate the sorting functions in C++.
Example
Output:
Sorted vector: 1 2 3 4 5 6 7 8 9 Stable sorted vector of pairs: (1,b) (2,d) (3,a) (3,c) Partially sorted (first 5 smallest): 1 2 3 4 5 9 8 7 6 Partial sort copy (5 smallest): 1 2 3 4 5 Is the sorted vector actually sorted? true is_sorted_until found unsorted at position: 4 After nth_element (5th smallest in correct place): 3 2 1 4 5 7 6 9 8 5th smallest element is: 5
Binary Search
The given table shows several binary search operations that are used in C++ Algorithm.
| Function | Description |
|---|---|
| lower_bound | Returns the lower bound element of the range. |
| upper_bound | Returns the upper bound element of the range. |
| equal_range | The function returns the subrange for the equal elements. |
| binary_search | The function tests if the values in the range exist in a sorted sequence or not. |
C++ Binary Search Operations Example
Let us take an example to demonstrate the binary search operations in C++.
Example
Output:
Sorted data: 1 3 3 3 5 7 9 11 Is value 3 present? true Lower bound of 3 is at index: 1, value: 3 Upper bound of 3 is at index: 4, value: 5 Equal range of 3 is from index 1 to 4 Elements in equal range: 3 3 3
Merge
The given table shows several merge operations that are used in C++ Algorithm.
Merge
| Function | Description |
|---|---|
| merge | The function merges two ranges that are in a sorted order. |
| inplace_merge | The function merges two consecutive ranges that are sorted. |
| includes | The function searches whether the sorted range includes another range or not. |
| set_union | The function returns the union of two ranges that are sorted. |
| set_intersection | The function returns the intersection of two ranges that are sorted. |
| set_difference | The function returns the difference of two ranges that are sorted. |
| set_symmetric_difference | The function returns the symmetric difference of two ranges that are sorted. |
C++ Merge Operations Example
Let us take an example to demonstrate the merge operations in C++.
Example
Output:
Merged A and B: 1 2 3 3 5 6 7 8 After inplace_merge: 1 2 3 4 5 6 7 Does superset include subset? true Set union: 1 2 3 5 6 7 8 Set intersection: 3 Set difference (A - B): 1 5 7 Set symmetric difference: 1 2 5 6 7 8
Heap
The given table shows several heap operations that are used in the C++ Algorithm.
| Function | Description |
|---|---|
| push_heap | The function pushes new elements in the heap. |
| pop_heap | The function pops new elements in the heap. |
| make_heap | The function is used for the creation of a heap. |
| sort_heap | The function sorts the heap. |
| is_heap | The function checks whether the range is a heap. |
| is_heap_until | The function checks till which position a range is a heap. |
C++ Heap Operations Example
Let us take an example to demonstrate the heap operations in C++.
Example
Output:
Heap after make_heap: 30 20 10 5 15 Heap after push_heap(25): 30 20 25 5 15 10 Popped element: 30 Heap after pop_heap: 25 20 10 5 15 Is the vector a heap? true is_heap_until fails at index: 6 Heap after sort_heap (sorted array): 5 10 15 20 25
Min/Max
The given table shows several min/max operations that are used in C++ Algorithm.
| Function | Description |
|---|---|
| min | Returns the smallest element of the range. |
| max | Returns the largest element of the range. |
| minmax | Returns the smallest and largest element of the range. |
| min_element | Returns the smallest element of the range. |
| max_element | Returns the largest element of the range. |
| minmax_element | Returns the smallest and largest element of the range. |
C++ Min/Max Operations Example
Let us take an example to demonstrate the min/max operations in C++.
Example
Output:
min(10, 20): 10 max(10, 20): 20 minmax(10, 20): (10, 20) Minimum element in vector: 9 at index 7 Maximum element in vector: 89 at index 5 Minmax in vector: min = 9, max = 89
Other Functions
The given table shows several other operations that are useful in C++ Algorithm.
| Function | Description |
|---|---|
| lexicographical_comapre | The function performs the lexicographical less-than comparison. |
| next_permutation | The function is used for the transformation of the range into the next permutation. |
| perv_permutation | The function is used for the transformation of the range into the previous permutation. |
Key features of C++ Algorithm Functions
There are several key features of C++ algorithm functions. Some main features are as follows:
- Generic and template-based: C++ algorithm functions are template-based, which allows them to work without re-writing with various data types and container types (such as vector, list, and array).
- Modifying and Non-Modifying Categories: The algorithm is grouped into non-modifying (such as find, count, and for_each) and modified (such as copy, replace, and transform) whether they change input data.
- Custom condition support: Many specific terms (eg, find_if, count_if) to define predicate functions or Lambdas, increase custom logic.
- Efficient and optimized: STL algorithms are adapted for performance and usually provide guaranteed time complexity (such as sort uses a hybrid sorting algorithm with complexity O(n log n).
- Do not resize containers: Algorithm containers do not manage size or memory.
Conclusion
In conclusion, the C++ algorithm function is an essential toolset for efficient and expressive programming, provided through the header in the standard template library (STL). They abstract low-level iteration logic and provide clean, customized operations for tasks such as searching, sorting, modifying, comparing, and transforming data.
Next TopicC++ Interview Questions and Answers