C++ algorithm library

The C++ Standard Library’s <algorithm> header provides a comprehensive suite of functions designed to operate on sequences of elements, commonly referred to as ranges. These functions facilitate a wide array of operations, including searching, sorting, counting, and manipulating data.


Non-Modifying Sequence Operations

FunctionDescription
std::all_ofReturns true if all elements in the range satisfy the given predicate.
std::any_ofReturns true if any element in the range satisfies the given predicate.
std::none_ofReturns true if no elements in the range satisfy the given predicate.
std::for_eachApplies a function to each element in the range.
std::findSearches for the first occurrence of a specified value in the range.
std::find_ifSearches for the first element in the range that satisfies the given predicate.
std::find_if_notSearches for the first element in the range that does not satisfy the given predicate.
std::find_endSearches for the last occurrence of a subsequence in the range.
std::find_first_ofSearches for the first element in the range that matches any element from another range.
std::adjacent_findSearches for the first occurrence of consecutive identical elements in the range.
std::countReturns the number of elements in the range that are equal to a specified value.
std::count_ifReturns the number of elements in the range that satisfy the given predicate.
std::mismatchFinds the first position where two ranges differ.
std::equalChecks if two ranges are equal.
std::is_permutationChecks if one range is a permutation of another.
std::searchSearches for the first occurrence of a subsequence within the range.
std::search_nSearches for the first occurrence of n consecutive elements that are equal to a specified value.

Modifying Sequence Operations

FunctionDescription
std::copyCopies elements from one range to another.
std::copy_nCopies a specified number of elements from one range to another.
std::copy_ifCopies elements from a range that satisfy a given predicate.
std::copy_backwardCopies elements from one range to another, starting from the end.
std::moveMoves elements from one range to another.
std::move_backwardMoves elements from one range to another, starting from the end.
std::swapExchanges the values of two objects.
std::swap_rangesExchanges the values of two ranges.
std::iter_swapExchanges the values of the elements pointed to by two iterators.
std::transformApplies a function to each element in a range and writes the result to another range.
std::replaceReplaces all occurrences of a value in a range with another value.
std::replace_ifReplaces elements in a range that satisfy a given predicate with another value.
std::replace_copyCopies a range, replacing all occurrences of a value in the new range.
std::replace_copy_ifCopies a range, replacing elements that satisfy a given predicate in the new range.
std::fillFills a range with a specified value.
std::fill_nFills a specified number of elements in a range with a value.
std::generateGenerates values for a range using a function.
std::generate_nGenerates values for a specified number of elements in a range using a function.
std::removeRemoves elements with a specific value from a range (logically, without changing the container size).
std::remove_ifRemoves elements from a range that satisfy a given predicate.
std::remove_copyCopies a range, excluding elements with a specific value.
std::remove_copy_ifCopies a range, excluding elements that satisfy a given predicate.
std::uniqueRemoves consecutive duplicate elements in a range.
std::unique_copyCopies a range, removing consecutive duplicates in the new range.
std::reverseReverses the elements in a range.
std::reverse_copyCopies a range, reversing the elements in the new range.
std::rotateRotates the elements in a range to the left.
std::rotate_copyCopies a range, rotating the elements in the new range to the left.
std::random_shuffleRandomly rearranges elements in a range.
std::shuffleRandomly rearranges elements in a range using a specified random generator.

Partitions

FunctionDescription
std::is_partitionedTests whether the range is partitioned according to a specified predicate.
std::partitionRearranges the elements in the range so that elements satisfying a predicate appear before those that do not.
std::stable_partitionPartitions the range into two groups while preserving the relative order of elements in each group.
std::partition_copyCopies elements from the input range into two separate ranges based on a predicate.
std::partition_pointReturns an iterator to the partition point of a range that has already been partitioned.

Sorting

FunctionDescription
std::sortSorts the elements in the range in ascending order.
std::stable_sortSorts the elements in the range while preserving the relative order of equivalent elements.
std::partial_sortSorts the first n elements in the range, leaving the rest unsorted.
std::partial_sort_copyCopies and partially sorts the elements from the input range to another range.
std::is_sortedChecks if the elements in the range are sorted.
std::is_sorted_untilFinds the first unsorted element in the range.
std::nth_elementRearranges the elements in the range so that the nth element is in its sorted position.

Binary Search (Operating on Partitioned/Sorted Ranges)

FunctionDescription
std::lower_boundReturns an iterator to the first element that is not less than the specified value in a sorted range.
std::upper_boundReturns an iterator to the first element that is greater than the specified value in a sorted range.
std::equal_rangeReturns a pair of iterators defining the subrange of elements that are equal to a specified value.
std::binary_searchTests if a specified value exists in a sorted sequence.

Merge (Operating on Sorted Ranges)

FunctionDescription
std::mergeMerges two sorted ranges into a single sorted range.
std::inplace_mergeMerges two consecutive sorted ranges within the same container.
std::includesChecks if one sorted range includes all elements of another sorted range.
std::set_unionComputes the union of two sorted ranges.
std::set_intersectionComputes the intersection of two sorted ranges.
std::set_differenceComputes the difference of two sorted ranges.
std::set_symmetric_differenceComputes the symmetric difference of two sorted ranges.

Heap Operations

FunctionDescription
std::push_heapInserts an element into a heap range and maintains the heap property.
std::pop_heapRemoves the largest element from a heap range.
std::make_heapTransforms a range into a valid heap.
std::sort_heapSorts the elements of a heap in ascending order.
std::is_heapChecks if a range satisfies the heap property.
std::is_heap_untilFinds the first element in a range that violates the heap property.

Min/Max Operations

FunctionDescription
std::minReturns the smallest of two or more values.
std::maxReturns the largest of two or more values.
std::minmaxReturns the smallest and largest of two or more values as a pair.
std::min_elementReturns an iterator to the smallest element in a range.
std::max_elementReturns an iterator to the largest element in a range.
std::minmax_elementReturns iterators to the smallest and largest elements in a range as a pair.

Other Operations

FunctionDescription
std::lexicographical_compareCompares two ranges lexicographically.
std::next_permutationTransforms the range into the next permutation in lexicographical order.
std::prev_permutationTransforms the range into the previous permutation in lexicographical order.

Key Features of the algorithm Library

  • Non-Modifying Sequence Operations: Functions that analyze or search ranges without altering their elements.
  • Modifying Sequence Operations: Functions that modify the elements within a range.
  • Sorting and Related Operations: Functions that order elements within a range or perform operations related to sorting.
  • Set Operations: Functions that perform standard set operations on sorted ranges.
  • Heap Operations: Functions that manage heap structures within ranges.
  • Minimum and Maximum Operations: Functions that determine the smallest or largest elements in a range.