当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::is_sorted用法及代码示例


C++函数std::is_sorted检查范围[first,last]中的元素是否按升序排序。使用<运算符比较元素。 std::is_sorted有两种变体:

  1. 不使用二元谓词
    bool is_sorted( ForwardIt first, ForwardIt last );
    first, last: the range of elements to examine
    返回值: 
    true: if the elements are in non-decreasing order.
    false: any element in increasing order.
    

    例:
    给定一个大小为n且范围在[0…n]之间的容器,编写一个程序来检查它是否按升序排序。数组中允许相等值,并且认为两个连续的相等值已排序。

    Input:2 5 9 4      /*Range = 3*/
    Output:Sorted in given range.
    
    Input:3 5 1 9     /*Range = 3*/
    Output:Not sorted in given range.
    
    // CPP program to illustrate 
    // std::is_sorted 
    // without binary predicate 
    #include <iostream> 
    #include <algorithm> 
      
    // Driver Code 
    int main() 
    { 
        int A[] = { 10, 11, 15, 12 }; 
      
        // Index 0 to 2 
        int range1 = 3; 
      
        // Index 0 to 3 
        int range2 = 4; 
      
        // Condition if container is sorted or not in range1 
        if (std::is_sorted(A, A + range1)) { 
            std::cout << "Sorted in the range:" << range1 << std::endl; 
        } else { 
            std::cout << "Not Sorted in the range:" << range1 << std::endl; 
        } 
      
        // Condition if container is sorted or not in range2 
        if (std::is_sorted(A, A + range2)) { 
            std::cout << "Sorted in the range:" << range2 << std::endl; 
        } else { 
            std::cout << "Not Sorted in the range:" << range2 << std::endl; 
        } 
        return 0; 
    }

    输出:


    Sorted in the range:3
    Not Sorted in the range:4
    

    我们在这里讨论了其他方法。

  2. 使用二进制谓词
    bool is_sorted (ForwardIt first, ForwardIt last, Compare comp);
    first, last: the range of elements to examine
    comp: binary predicate
    返回值: 
    true: if the elements are in non-decreasing order.
    false: any element in increasing order.
    

    例:
    给定一个仅由字符组成的字符串。检查字符是否按顺序排列。另外,在比较时忽略案例,即“ f”>“ A”

    Input:AHZP
    Output:Not Sorted
    
    Input:Boy
    Output:Sorted
    
    // CPP program to illustrate 
    // std::is_sorted 
    // using binary predicate 
    #include <iostream> 
    #include <algorithm> 
      
    using namespace std; 
      
    // Binary predicate 
    bool ignore_case(char a, char b) 
    { 
        // Converts both characters to lowercase and checks if a <= b 
        return (tolower(a) <= tolower(b)); 
    } 
      
    // Function that checks if string is sorted while ignoring the case 
    bool check_if_sorted(string str) 
    { 
        // Function call to is_sorted with binary predicate ignore_case 
        return is_sorted(str.begin(), str.end(), ignore_case); 
    } 
      
    // Driver code 
    int main() 
    { 
        // String which is to be checked 
        string str = "tOY"; 
      
        // Function returned true, string is sorted 
        if (check_if_sorted(str)) { 
            cout << "Sorted"; 
        } 
        // Function returned false, string not sorted 
        else { 
            cout << "Not sorted"; 
        } 
      
        return 0; 
    }

    输出:

    Sorted
    


相关用法


注:本文由纯净天空筛选整理自 std::is_sorted in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。