當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。