Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Algorithms library

      From cppreference.com
      <cpp
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       

      The algorithms library defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on ranges of elements. Note that arange is defined as[firstlast) wherelast refers to the elementpast the last element to inspect or modify.

      Contents

      [edit]Constrained algorithms(since C++20)

      C++20 providesconstrained versions of most algorithms in the namespacestd::ranges. In these algorithms, arange can be specified as either aniterator-sentinel pair or as a singlerange argument, and projections and pointer-to-member callables are supported. Additionally, thereturn types of most algorithms have been changed to return all potentially useful information computed during the execution of the algorithm.

      std::vector<int> v{7,1,4,0,-1};std::ranges::sort(v);// constrained algorithm

      [edit]Execution policies(since C++17)

      Most algorithms have overloads that accept execution policies. The standard library algorithms support severalexecution policies, and the library provides corresponding execution policy types and objects. Users may select an execution policy statically by invoking a parallel algorithm with anexecution policy object of the corresponding type.

      Standard library implementations (but not the users) may define additional execution policies as an extension. The semantics of parallel algorithms invoked with an execution policy object of implementation-defined type is implementation-defined.

      Parallel version of algorithms (except forstd::for_each andstd::for_each_n) are allowed to make arbitrary copies of elements from ranges, as long as bothstd::is_trivially_copy_constructible_v<T> andstd::is_trivially_destructible_v<T> aretrue, whereT is the type of elements.

      Defined in header<execution>
      Defined in namespacestd::execution
      execution policy types
      (class)[edit]
      (C++17)(C++17)(C++17)(C++20)
      global execution policy objects
      (constant)[edit]
      Defined in namespacestd
      test whether a class represents an execution policy
      (class template)[edit]
      Feature-test macroValueStdFeature
      __cpp_lib_parallel_algorithm201603L(C++17)Parallel algorithms
      __cpp_lib_execution201603L(C++17)Execution policies
      201902L(C++20)std::execution::unsequenced_policy

      [edit]Non-modifying sequence operations

      [edit]Batch operations

      Defined in header<algorithm>
      applies a unaryfunction object to elements from arange
      (function template)[edit]
      applies a unaryfunction object to elements from arange
      (algorithm function object)[edit]
      (C++17)
      applies a function object to the first N elements of a sequence
      (function template)[edit]
      applies a function object to the first N elements of a sequence
      (algorithm function object)[edit]

      [edit]Search operations

      Defined in header<algorithm>
      (C++11)(C++11)(C++11)
      checks if a predicate istrue for all, any or none of the elements in a range
      (function template)[edit]
      checks if a predicate istrue for all, any or none of the elements in a range
      (algorithm function object)[edit]
      checks if the range contains the given element or subrange
      (algorithm function object)[edit]
      finds the first element satisfying specific criteria
      (function template)[edit]
      finds the first element satisfying specific criteria
      (algorithm function object)[edit]
      finds the last element satisfying specific criteria
      (algorithm function object)[edit]
      finds the last sequence of elements in a certain range
      (function template)[edit]
      finds the last sequence of elements in a certain range
      (algorithm function object)[edit]
      searches for any one of a set of elements
      (function template)[edit]
      searches for any one of a set of elements
      (algorithm function object)[edit]
      finds the first two adjacent items that are equal (or satisfy a given predicate)
      (function template)[edit]
      finds the first two adjacent items that are equal (or satisfy a given predicate)
      (algorithm function object)[edit]
      returns the number of elements satisfying specific criteria
      (function template)[edit]
      returns the number of elements satisfying specific criteria
      (algorithm function object)[edit]
      finds the first position where two ranges differ
      (function template)[edit]
      finds the first position where two ranges differ
      (algorithm function object)[edit]
      determines if two sets of elements are the same
      (function template)[edit]
      determines if two sets of elements are the same
      (algorithm function object)[edit]
      searches for the first occurrence of a range of elements
      (function template)[edit]
      searches for the first occurrence of a range of elements
      (algorithm function object)[edit]
      searches for the first occurrence of a number consecutive copies of an element in a range
      (function template)[edit]
      searches for the first occurrence of a number consecutive copies of an element in a range
      (algorithm function object)[edit]
      checks whether a range starts with another range
      (algorithm function object)[edit]
      checks whether a range ends with another range
      (algorithm function object)[edit]

      [edit]Fold operations(since C++23)

      Defined in header<algorithm>
      left-folds a range of elements
      (algorithm function object)[edit]
      left-folds a range of elements using the first element as an initial value
      (algorithm function object)[edit]
      right-folds a range of elements
      (algorithm function object)[edit]
      right-folds a range of elements using the last element as an initial value
      (algorithm function object)[edit]
      left-folds a range of elements, and returns apair (iterator, value)
      (algorithm function object)[edit]
      left-folds a range of elements using the first element as an initial value, and returns apair (iterator,optional)
      (algorithm function object)[edit]

      [edit]Modifying sequence operations

      [edit]Copy operations

      Defined in header<algorithm>
      copies a range of elements to a new location
      (function template)[edit]
      copies a range of elements to a new location
      (algorithm function object)[edit]
      (C++11)
      copies a number of elements to a new location
      (function template)[edit]
      copies a number of elements to a new location
      (algorithm function object)[edit]
      copies a range of elements in backwards order
      (function template)[edit]
      copies a range of elements in backwards order
      (algorithm function object)[edit]
      (C++11)
      moves a range of elements to a new location
      (function template)[edit]
      moves a range of elements to a new location
      (algorithm function object)[edit]
      moves a range of elements to a new location in backwards order
      (function template)[edit]
      moves a range of elements to a new location in backwards order
      (algorithm function object)[edit]

      [edit]Swap operations

      Defined in header<algorithm>   (until C++11)
      Defined in header<utility>     (since C++11)
      Defined in header<string_view>
      swaps the values of two objects
      (function template)[edit]
      Defined in header<algorithm>
      swaps two ranges of elements
      (function template)[edit]
      swaps two ranges of elements
      (algorithm function object)[edit]
      swaps the elements pointed to by two iterators
      (function template)[edit]

      [edit]Transformation operations

      Defined in header<algorithm>
      applies a function to a range of elements, storing results in a destination range
      (function template)[edit]
      applies a function to a range of elements
      (algorithm function object)[edit]
      replaces all values satisfying specific criteria with another value
      (function template)[edit]
      replaces all values satisfying specific criteria with another value
      (algorithm function object)[edit]
      copies a range, replacing elements satisfying specific criteria with another value
      (function template)[edit]
      copies a range, replacing elements satisfying specific criteria with another value
      (algorithm function object)[edit]

      [edit]Generation operations

      Defined in header<algorithm>
      copy-assigns the given value to every element in a range
      (function template)[edit]
      assigns a range of elements a certain value
      (algorithm function object)[edit]
      copy-assigns the given value to N elements in a range
      (function template)[edit]
      assigns a value to a number of elements
      (algorithm function object)[edit]
      assigns the results of successive function calls to every element in a range
      (function template)[edit]
      saves the result of a function in a range
      (algorithm function object)[edit]
      assigns the results of successive function calls to N elements in a range
      (function template)[edit]
      saves the result of N applications of a function
      (algorithm function object)[edit]

      [edit]Removing operations

      Defined in header<algorithm>
      removes elements satisfying specific criteria
      (function template)[edit]
      removes elements satisfying specific criteria
      (algorithm function object)[edit]
      copies a range of elements omitting those that satisfy specific criteria
      (function template)[edit]
      copies a range of elements omitting those that satisfy specific criteria
      (algorithm function object)[edit]
      removes consecutive duplicate elements in a range
      (function template)[edit]
      removes consecutive duplicate elements in a range
      (algorithm function object)[edit]
      creates a copy of some range of elements that contains no consecutive duplicates
      (function template)[edit]
      creates a copy of some range of elements that contains no consecutive duplicates
      (algorithm function object)[edit]

      [edit]Order-changing operations

      Defined in header<algorithm>
      reverses the order of elements in a range
      (function template)[edit]
      reverses the order of elements in a range
      (algorithm function object)[edit]
      creates a copy of a range that is reversed
      (function template)[edit]
      creates a copy of a range that is reversed
      (algorithm function object)[edit]
      rotates the order of elements in a range
      (function template)[edit]
      rotates the order of elements in a range
      (algorithm function object)[edit]
      copies and rotate a range of elements
      (function template)[edit]
      copies and rotate a range of elements
      (algorithm function object)[edit]
      shifts elements in a range
      (function template)[edit]
      shifts elements in a range
      (algorithm function object)[edit]
      (until C++17)(C++11)
      randomly re-orders elements in a range
      (function template)[edit]
      randomly re-orders elements in a range
      (algorithm function object)[edit]

      [edit]Sampling operations

      Defined in header<algorithm>
      (C++17)
      selects N random elements from a sequence
      (function template)[edit]
      selects N random elements from a sequence
      (algorithm function object)[edit]

      [edit]Sorting and related operations

      [edit]Requirements

      Some algorithms require the sequence represented by the arguments to be “sorted” or “partitioned”. The behavior is undefined if the requirement is not met.

      A sequence issorted with respect to a comparatorcomp if for every iteratoriter pointing to the sequence and every non-negative integern such thatiter+ n[1] is avalid iterator pointing to an element of the sequence,comp(*(iter+ n),*iter)==false[1].

      (until C++20)

      A sequence issorted with respect tocomp andproj for a comparatorcomp and projectionproj if for every iteratoriter pointing to the sequence and every non-negative integern such thatiter+ n[1] is a valid iterator pointing to an element of the sequence,bool(std::invoke(comp,std::invoke(proj,*(iter+ n)),
                             std::invoke(proj,*iter)))
      [1] isfalse.

      A sequence issorted with respect to a comparatorcomp if the sequence is sorted with respect tocomp andstd::identity{} (the identity projection).

      (since C++20)

      A sequence[startfinish) ispartitioned with respect to an expressionf(e) if there exists an integern such that for alli in[0std::distance(start, finish)),f(*(start+ i))[1] istrue if and only ifi< n.

      1. 1.01.11.21.31.4iter+ n simply means “the result ofiter being incrementedn times”, regardless of whetheriter is a random access iterator.

      [edit]Partitioning operations

      Defined in header<algorithm>
      determines if the range is partitioned by the given predicate
      (function template)[edit]
      determines if the range is partitioned by the given predicate
      (algorithm function object)[edit]
      divides a range of elements into two groups
      (function template)[edit]
      divides a range of elements into two groups
      (algorithm function object)[edit]
      copies a range dividing the elements into two groups
      (function template)[edit]
      copies a range dividing the elements into two groups
      (algorithm function object)[edit]
      divides elements into two groups while preserving their relative order
      (function template)[edit]
      divides elements into two groups while preserving their relative order
      (algorithm function object)[edit]
      locates the partition point of a partitioned range
      (function template)[edit]
      locates the partition point of a partitioned range
      (algorithm function object)[edit]

      [edit]Sorting operations

      Defined in header<algorithm>
      sorts a range into ascending order
      (function template)[edit]
      sorts a range into ascending order
      (algorithm function object)[edit]
      sorts a range of elements while preserving order between equal elements
      (function template)[edit]
      sorts a range of elements while preserving order between equal elements
      (algorithm function object)[edit]
      sorts the first N elements of a range
      (function template)[edit]
      sorts the first N elements of a range
      (algorithm function object)[edit]
      copies and partially sorts a range of elements
      (function template)[edit]
      copies and partially sorts a range of elements
      (algorithm function object)[edit]
      (C++11)
      checks whether a range is sorted into ascending order
      (function template)[edit]
      checks whether a range is sorted into ascending order
      (algorithm function object)[edit]
      finds the largest sorted subrange
      (function template)[edit]
      finds the largest sorted subrange
      (algorithm function object)[edit]
      partially sorts the given range making sure that it is partitioned by the given element
      (function template)[edit]
      partially sorts the given range making sure that it is partitioned by the given element
      (algorithm function object)[edit]

      [edit]Binary search operations (on partitioned ranges)

      Defined in header<algorithm>
      returns an iterator to the first elementnot less than the given value
      (function template)[edit]
      returns an iterator to the first elementnot less than the given value
      (algorithm function object)[edit]
      returns an iterator to the first elementgreater than a certain value
      (function template)[edit]
      returns an iterator to the first elementgreater than a certain value
      (algorithm function object)[edit]
      returns range of elements matching a specific key
      (function template)[edit]
      returns range of elements matching a specific key
      (algorithm function object)[edit]
      determines if an element exists in a partially-ordered range
      (function template)[edit]
      determines if an element exists in a partially-ordered range
      (algorithm function object)[edit]

      [edit]Set operations (on sorted ranges)

      Defined in header<algorithm>
      returnstrue if one sequence is a subsequence of another
      (function template)[edit]
      returnstrue if one sequence is a subsequence of another
      (algorithm function object)[edit]
      computes the union of two sets
      (function template)[edit]
      computes the union of two sets
      (algorithm function object)[edit]
      computes the intersection of two sets
      (function template)[edit]
      computes the intersection of two sets
      (algorithm function object)[edit]
      computes the difference between two sets
      (function template)[edit]
      computes the difference between two sets
      (algorithm function object)[edit]
      computes the symmetric difference between two sets
      (function template)[edit]
      computes the symmetric difference between two sets
      (algorithm function object)[edit]

      [edit]Merge operations (on sorted ranges)

      Defined in header<algorithm>
      merges two sorted ranges
      (function template)[edit]
      merges two sorted ranges
      (algorithm function object)[edit]
      merges two ordered ranges in-place
      (function template)[edit]
      merges two ordered ranges in-place
      (algorithm function object)[edit]

      [edit]Heap operations

      A random accessrange[firstlast) is aheap with respect to a comparatorcomp ifbool(comp(first[(i-1)/2], first[i])) isfalse for all integeri in(0last- first).

      (until C++20)

      A random accessrange[firstlast) is aheap with respect tocomp andproj for a comparatorcomp and projectionproj ifbool(std::invoke(comp,std::invoke(proj, first[(i-1)/2]),
                             std::invoke(proj, first[i]))
      isfalse for all integeri in(0last- first).

      A random accessrange[firstlast) is aheap with respect to a comparatorcomp if the range is a heap with respect tocomp andstd::identity{} (the identity projection).

      (since C++20)

      A heap can be created bystd::make_heap andranges::make_heap(since C++20).

      For more properties of heap, seemax heap.


      Defined in header<algorithm>
      adds an element to a max heap
      (function template)[edit]
      adds an element to a max heap
      (algorithm function object)[edit]
      removes the largest element from a max heap
      (function template)[edit]
      removes the largest element from a max heap
      (algorithm function object)[edit]
      creates a max heap out of a range of elements
      (function template)[edit]
      creates a max heap out of a range of elements
      (algorithm function object)[edit]
      turns a max heap into a range of elements sorted in ascending order
      (function template)[edit]
      turns a max heap into a range of elements sorted in ascending order
      (algorithm function object)[edit]
      (C++11)
      checks if the given range is a max heap
      (function template)[edit]
      checks if the given range is a max heap
      (algorithm function object)[edit]
      finds the largest subrange that is a max heap
      (function template)[edit]
      finds the largest subrange that is a max heap
      (algorithm function object)[edit]

      [edit]Minimum/maximum operations

      Defined in header<algorithm>
      returns the greater of the given values
      (function template)[edit]
      returns the greater of the given values
      (algorithm function object)[edit]
      returns the largest element in a range
      (function template)[edit]
      returns the largest element in a range
      (algorithm function object)[edit]
      returns the smaller of the given values
      (function template)[edit]
      returns the smaller of the given values
      (algorithm function object)[edit]
      returns the smallest element in a range
      (function template)[edit]
      returns the smallest element in a range
      (algorithm function object)[edit]
      (C++11)
      returns the smaller and larger of two elements
      (function template)[edit]
      returns the smaller and larger of two elements
      (algorithm function object)[edit]
      returns the smallest and the largest elements in a range
      (function template)[edit]
      returns the smallest and the largest elements in a range
      (algorithm function object)[edit]
      (C++17)
      clamps a value between a pair of boundary values
      (function template)[edit]
      clamps a value between a pair of boundary values
      (algorithm function object)[edit]

      [edit]Lexicographical comparison operations

      Defined in header<algorithm>
      returnstrue if one range is lexicographically less than another
      (function template)[edit]
      returnstrue if one range is lexicographically less than another
      (algorithm function object)[edit]
      compares two ranges using three-way comparison
      (function template)[edit]

      [edit]Permutation operations

      Defined in header<algorithm>
      generates the next greater lexicographic permutation of a range of elements
      (function template)[edit]
      generates the next greater lexicographic permutation of a range of elements
      (algorithm function object)[edit]
      generates the next smaller lexicographic permutation of a range of elements
      (function template)[edit]
      generates the next smaller lexicographic permutation of a range of elements
      (algorithm function object)[edit]
      determines if a sequence is a permutation of another sequence
      (function template)[edit]
      determines if a sequence is a permutation of another sequence
      (algorithm function object)[edit]

      [edit]Numeric operations

      Defined in header<numeric>
      (C++11)
      fills a range with successive increments of the starting value
      (function template)[edit]
      fills a range with successive increments of the starting value
      (algorithm function object)[edit]
      sums up or folds a range of elements
      (function template)[edit]
      computes the inner product of two ranges of elements
      (function template)[edit]
      computes the differences between adjacent elements in a range
      (function template)[edit]
      computes the partial sum of a range of elements
      (function template)[edit]
      (C++17)
      similar tostd::accumulate, except out of order
      (function template)[edit]
      similar tostd::partial_sum, excludes theith input element from theith sum
      (function template)[edit]
      similar tostd::partial_sum, includes theith input element in theith sum
      (function template)[edit]
      applies an invocable, then reduces out of order
      (function template)[edit]
      applies an invocable, then calculates exclusive scan
      (function template)[edit]
      applies an invocable, then calculates inclusive scan
      (function template)[edit]

      [edit]Operations on uninitialized memory

      Defined in header<memory>
      copies a range of objects to an uninitialized area of memory
      (function template)[edit]
      copies a range of objects to an uninitialized area of memory
      (algorithm function object)[edit]
      copies a number of objects to an uninitialized area of memory
      (function template)[edit]
      copies a number of objects to an uninitialized area of memory
      (algorithm function object)[edit]
      copies an object to an uninitialized area of memory, defined by a range
      (function template)[edit]
      copies an object to an uninitialized area of memory, defined by a range
      (algorithm function object)[edit]
      copies an object to an uninitialized area of memory, defined by a start and a count
      (function template)[edit]
      copies an object to an uninitialized area of memory, defined by a start and a count
      (algorithm function object)[edit]
      moves a range of objects to an uninitialized area of memory
      (function template)[edit]
      moves a range of objects to an uninitialized area of memory
      (algorithm function object)[edit]
      moves a number of objects to an uninitialized area of memory
      (function template)[edit]
      moves a number of objects to an uninitialized area of memory
      (algorithm function object)[edit]
      constructs objects bydefault-initialization in an uninitialized area of memory, defined by a range
      (function template)[edit]
      constructs objects bydefault-initialization in an uninitialized area of memory, defined by a range
      (algorithm function object)[edit]
      constructs objects bydefault-initialization in an uninitialized area of memory, defined by a start and a count
      (function template)[edit]
      constructs objects bydefault-initialization in an uninitialized area of memory, defined by a start and count
      (algorithm function object)[edit]
      constructs objects byvalue-initialization in an uninitialized area of memory, defined by a range
      (function template)[edit]
      constructs objects byvalue-initialization in an uninitialized area of memory, defined by a range
      (algorithm function object)[edit]
      constructs objects byvalue-initialization in an uninitialized area of memory, defined by a start and a count
      (function template)[edit]
      constructs objects byvalue-initialization in an uninitialized area of memory, defined by a start and a count
      (algorithm function object)[edit]
      (C++17)
      destroys a range of objects
      (function template)[edit]
      destroys a range of objects
      (algorithm function object)[edit]
      (C++17)
      destroys a number of objects in a range
      (function template)[edit]
      destroys a number of objects in a range
      (algorithm function object)[edit]
      (C++17)
      destroys an object at a given address
      (function template)[edit]
      destroys an object at a given address
      (algorithm function object)[edit]
      creates an object at a given address
      (function template)[edit]
      creates an object at a given address
      (algorithm function object)[edit]

      [edit]Random number generation(since C++26)

      Defined in header<random>
      fills a range with random numbers from a uniform random bit generator
      (algorithm function object)[edit]

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_algorithm_iterator_requirements202207L(C++23)Ranges iterators as inputs to non-Ranges algorithms
      __cpp_lib_clamp201603L(C++17)std::clamp
      __cpp_lib_constexpr_algorithms201806L(C++20)Constexpr for algorithms
      202306L(C++26)Constexpr stable sorting
      __cpp_lib_algorithm_default_value_type202403L(C++26)List-initialization for algorithms
      __cpp_lib_freestanding_algorithm202311L(C++26)Freestanding facilities in<algorithm>
      __cpp_lib_robust_nonmodifying_seq_ops201304L(C++14)Making non-modifying sequence operations more robust (two-range overloads forstd::mismatch,std::equal andstd::is_permutation)
      __cpp_lib_sample201603L(C++17)std::sample
      __cpp_lib_shift201806L(C++20)std::shift_left andstd::shift_right

      [edit]C library

      Defined in header<cstdlib>
      sorts a range of elements with unspecified type
      (function)[edit]
      searches an array for an element of unspecified type
      (function)[edit]

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 193C++98heap required*first to be the largest elementthere can be elements
      equal to*first
      LWG 2150C++98the definition of a sorted sequence was incorrectcorrected
      LWG 2166C++98the heap requirement did not match the
      definition ofmax heap closely enough
      requirement improved

      [edit]See also

      C documentation forAlgorithms
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm&oldid=180667"

      [8]ページ先頭

      ©2009-2025 Movatter.jp