一:功能
检查一个区间内的元素是否有序,按升序(或降序)排列
二:用法
#include <algorithm>
#include <iostream>
int main() {
std::vector<int> data1 = {1, 2, 3, 4, 5};
bool test1 = std::is_sorted(data1.begin(), data1.end());
std::cout << std::boolalpha <<
"is_sorted({1, 2, 3, 4, 5}) == " << test1 << "\n";
std::vector<int> data2 = {5, 4, 3, 2, 1};
bool test2 = std::ranges::is_sorted(data2);
std::cout << std::boolalpha <<
"is_sorted({5, 4, 3, 2, 1}) == " << test2 << "\n";
bool test3 = std::ranges::is_sorted(data2, std::greater<>{});
std::cout << std::boolalpha <<
"is_sorted({5, 4, 3, 2, 1}, greater<>{}) == " << test3 << "\n";
}
#include <algorithm>
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> v;
assert(std::is_sorted(v.cbegin(), v.cend()) && "an empty range is always sorted");
v.push_back(42);
assert(std::is_sorted(v.cbegin(), v.cend()) && "a range of size 1 is always sorted");
int data[] = {3, 1, 4, 1, 5};
assert(not std::is_sorted(std::begin(data), std::end(data)));
std::sort(std::begin(data), std::end(data));
assert(std::is_sorted(std::begin(data), std::end(data)));
assert(not std::is_sorted(std::begin(data), std::end(data), std::greater<>{}));
}
标签:std,begin,end,STL,C++,sorted,include,data From: https://blog.csdn.net/zg260/article/details/140435896