📜  C++中的std :: is_sorted

📅  最后修改于: 2021-05-30 07:40:28             🧑  作者: Mango

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
    Return value : 
    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 
    #include 
      
    // 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
    Return value : 
    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 
    #include 
      
    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;
    }
    

    输出 :

    Not sorted
    
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”