Constrained algorithms and algorithms on ranges(C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Constrained algorithms, e.g.ranges::copy,ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Execution policies(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on uninitialized memory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Defined in header <algorithm> | ||
template<class ForwardIt> bool is_sorted( ForwardIt first, ForwardIt last); | (1) | (since C++11) (constexpr since C++20) |
template<class ExecutionPolicy,class ForwardIt> bool is_sorted( ExecutionPolicy&& policy, | (2) | (since C++17) |
template<class ForwardIt,class Compare> bool is_sorted( ForwardIt first, ForwardIt last, Compare comp); | (3) | (since C++11) (constexpr since C++20) |
template<class ExecutionPolicy,class ForwardIt,class Compare> bool is_sorted( ExecutionPolicy&& policy, | (4) | (since C++17) |
Checks if the elements in range[
first,
last)
are sorted in non-descending order.
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> istrue. | (until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue. | (since C++20) |
Contents |
first, last | - | the pair of iterators defining therange of elements to examine |
policy | - | theexecution policy to use |
comp | - | comparison function object (i.e. an object that satisfies the requirements ofCompare) which returns true if the first argument isless than (i.e. is orderedbefore) the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1& a,const Type2& b); While the signature does not need to haveconst&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) |
Type requirements | ||
-ForwardIt must meet the requirements ofLegacyForwardIterator. | ||
-Compare must meet the requirements ofCompare. |
true if the elements in the range are sorted in non-descending order,false otherwise.
Given\(\scriptsize N\)N asstd::distance(first, last):
The overloads with a template parameter namedExecutionPolicy
report errors as follows:
ExecutionPolicy
is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined.See also the implementations inlibstdc++ andlibc++.
is_sorted (1) |
---|
template<class ForwardIt>bool is_sorted(ForwardIt first, ForwardIt last){returnstd::is_sorted_until(first, last)== last;} |
is_sorted (3) |
template<class ForwardIt,class Compare>bool is_sorted(ForwardIt first, ForwardIt last, Compare comp){returnstd::is_sorted_until(first, last, comp)== last;} |
std::is_sorted
returnstrue for empty ranges and ranges of length one.
#include <algorithm>#include <cassert>#include <functional>#include <iterator>#include <vector> int main(){std::vector<int> v;assert(std::is_sorted(v.cbegin(), v.cend())&&"an empty range is always sorted"); v.push_back(42);assert(std::is_sorted(v.cbegin(), v.cend())&&"a range of size 1 is always sorted"); int data[]={3,1,4,1,5};assert(not std::is_sorted(std::begin(data),std::end(data))); std::sort(std::begin(data),std::end(data));assert(std::is_sorted(std::begin(data),std::end(data)));assert(not std::is_sorted(std::begin(data),std::end(data),std::greater<>{}));}
(C++11) | finds the largest sorted subrange (function template)[edit] |
(C++20) | checks whether a range is sorted into ascending order (algorithm function object)[edit] |