std:: is_sorted
| 
           定义于头文件
            
            
             <algorithm>
            
            | ||
| 
           
           
           
            
             template
            
            
             <
            
            
             class
            
            ForwardIt
            
             >
            
             bool is_sorted ( ForwardIt first, ForwardIt last ) ; | (1) | (C++11 起) (C++20 起为 constexpr) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            ExecutionPolicy,
            
             class
            
            ForwardIt
            
             >
            
             
             
              bool
             
             is_sorted
             
              (
             
             ExecutionPolicy
             
              &&
             
             policy,
              | (2) | (C++17 起) | 
| 
           
           
           
            
             template
            
            
             <
            
            
             class
            
            ForwardIt,
            
             class
            
            Compare
            
             >
            
             bool is_sorted ( ForwardIt first, ForwardIt last, Compare comp ) ; | (3) | (C++11 起) (C++20 起为 constexpr) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            ExecutionPolicy,
            
             class
            
            ForwardIt,
            
             class
            
            Compare
            
             >
            
             
             
              bool
             
             is_sorted
             
              (
             
             ExecutionPolicy
             
              &&
             
             policy,
              | (4) | (C++17 起) | 
       检查范围
       
        
         [
        
        
         
          first
         
        
        
         ,
        
        
         
          last
         
        
        
         )
        
       
       中的元素是否按非降序排列。
      
| std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> 为 true 。 | (C++20 前) | 
| std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> 为 true 。 | (C++20 起) | 
| 目录 | 
参数
| first, last | - | 定义待检验元素范围的迭代器对 | 
| policy | - | 要使用的执行策略 | 
| comp | - | 比较函数对象(即满足比较要求对象),若第一参数小于(即先序于)第二参数则返回
         
          
           true
          
         
         。 比较函数的签名应等价于如下形式: bool cmp ( const Type1 & a, const Type2 & b ) ; 
          虽然签名不必包含
          
           
            const
           
           
            &
           
          
          ,函数也不能修改传递给它的对象,而且必须能够接受(可能为 const 的)类型
           | 
| 类型要求 | ||
| - 
          ForwardIt
         必须满足
         
          
           前向迭代器
          
         
         的要求。 | ||
| - 
          Compare
         必须满足
         
          
           比较
          
         
         的要求。 | ||
返回值
若范围内的元素按非降序排列则为 true ,否则为 false 。
复杂度
给定 N 为 std:: distance ( first, last ) :
异常
       带有名为
       
        ExecutionPolicy
       
       模板参数的重载按以下方式报告错误:
      
- 
        如果作为算法一部分调用的函数执行抛出异常,且
        ExecutionPolicy是某个 标准策略 ,则调用 std::terminate 。对于其他任何ExecutionPolicy,其行为由实现定义。
- 如果算法无法分配内存,则抛出 std::bad_alloc 。
可能的实现
       
      
| is_sorted (1) | 
|---|
| template<class ForwardIt> bool is_sorted(ForwardIt first, ForwardIt last) { return std::is_sorted_until(first, last) == last; } | 
| is_sorted (3) | 
| template<class ForwardIt, class Compare> bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) { return std::is_sorted_until(first, last, comp) == last; } | 
注释
       
        std::is_sorted
       
       对于空范围和长度为1的范围返回
       
        
         
          true
         
        
       
       。
      
示例
#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)
             
            
           
           | 寻找最大有序子范围 (函数模板) | 
| 
           
            
             
              (C++20)
             
            
           
           | 检查范围是否按升序排序 (算法函数对象) |