- C 标准库
- C 标准库
- C++ 标准库
- C++ 库 - 主页
- C++ 库 - <fstream>
- C++ 库 - <iomanip>
- C++ 库 - <ios>
- C++ 库 - <iosfwd>
- C++ 库 - <iostream>
- C++ 库 - <istream>
- C++ 库 - <ostream>
- C++ 库 - <sstream>
- C++ 库 - <streambuf>
- C++ 库 - <原子>
- C++ 库 - <复杂>
- C++ 库 - <异常>
- C++ 库 - <功能>
- C++ 库 - <限制>
- C++ 库 - <语言环境>
- C++ 库 - <内存>
- C++ 库 - <新>
- C++ 库 - <数字>
- C++ 库 - <正则表达式>
- C++ 库 - <stdexcept>
- C++ 库 - <字符串>
- C++ 库 - <线程>
- C++ 库 - <元组>
- C++ 库 - <类型信息>
- C++ 库 - <实用程序>
- C++ 库 - <valarray>
C++ 算法库 - is_sorted() 函数
描述
C++ 函数std::algorithm::is_sorted()测试范围是否已排序。它使用二元函数进行比较。
宣言
以下是 std::algorithm::is_sorted() 函数形式 std::algorithm 标头的声明。
C++11
template <class ForwardIterator, class Compare> bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
参数
first - 将迭代器转发到初始位置。
last - 将迭代器转发到最终位置。
comp - 接受两个参数并返回 bool 的二元函数。
返回值
如果范围已排序,则返回 true,否则返回 false。
例外情况
如果二元函数或迭代器上的操作抛出异常,则抛出异常。
请注意,无效参数会导致未定义的行为。
时间复杂度
线性。
例子
以下示例显示了 std::algorithm::is_sorted() 函数的用法。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool ignore_case(char a, char b) {
return (tolower(a) == tolower(b));
}
int main(void) {
vector<char> v = {'A', 'b', 'C', 'd', 'E'};
bool result;
result = is_sorted(v.begin(), v.end());
if (result == false)
cout << "Vector elements are not sorted in ascending order." << endl;
result = is_sorted(v.begin(), v.end(), ignore_case);
if (result == true)
cout << "Vector elements are sorted in ascending order." << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Vector elements are not sorted in ascending order. Vector elements are sorted in ascending order.
算法.htm