目录

algorithm::is_sorted_until()

描述 (Description)

C ++函数std::algorithm::is_sorted_until()从序列中查找第一个未排序的元素。 它使用运算符“进行比较。

声明 (Declaration)

以下是std :: algorithm :: is_sorted_until()函数形式std :: algorithm头的声明。

C++11

template <class ForwardIterator>
ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last);

参数 (Parameters)

  • first - 将迭代器转发到初始位置。

  • last - 将迭代器转发到最终位置。

返回值

返回第一个未排序元素的迭代器。 如果整个范围被排序,则它最后返回。

异常 (Exceptions)

如果元素比较或迭代器上的操作抛出异常,则抛出异常。

请注意,无效参数会导致未定义的行为。

时间复杂

线性。

例子 (Example)

以下示例显示了std :: algorithm :: is_sorted_until()函数的用法。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
   vector<int> v = {1, 2, 3, 5, 4};
   auto it = is_sorted_until(v.begin(), v.end());
   cout << "First unsorted element = " << *it << endl;
   v[3] = 4;
   it = is_sorted_until(v.begin(), v.end());
   if (it == end(v))
      cout << "Entire vector is sorted." << endl;
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

First unsorted element = 4
Entire vector is sorted.
↑回到顶部↑
WIKI教程 @2018