首页 > 编程语言 >C++ STL is_sorted用法

C++ STL is_sorted用法

时间:2024-07-15 12:55:21浏览次数:16  
标签:std begin end STL C++ sorted include data

一:功能

     检查一个区间内的元素是否有序,按升序(或降序)排列

二:用法

#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

相关文章

  • Hypertable 基于C++开发编译环境部署
    一、安装gccyuminstallgccgcc-c++二、安装boostyuminstallboostboost-develboost1.42以上版本,执行以下脚本:tarxjvfboost_1_44_0.tar.bz2cdboost_1_44_0./bootstrap.sh--with-libraries=filesystem,iostreams,program_options,system,thread,graph,regex./bjam......
  • C/C++ 避免缓冲区溢出的措施
    在C/C++中,缓冲区溢出是一种常见的安全问题,可以导致程序崩溃或安全漏洞。为了避免缓冲区溢出,可以采取以下防范措施:使用安全的函数:使用strncpy(), strncat(), snprintf()等函数代替strcpy(), strcat(), sprintf()等,这些函数允许你指定最大复制的字符数,从而避免溢出。......
  • C++11标准库 未来体 <future> 梳理
    目录<future>future模板类成员函数:promise类promise的使用例程:packaged_task模板类例程:async模板函数例程:shared_future模板类<future>标准库提供了一些工具来获取异步任务(即在单独的线程中启动的函数)的返回值,并捕捉其所抛出的异常。这些值在共享状态中传递,其中异步任务可以......
  • C++11标准库 条件变量 <condition_variable> 梳理
    目录<condition_variable>condition_variable类类方法生产者消费者模型--阻塞队列单条件变量版condition_variable_any模板类区别优缺点<condition_variable>条件变量是C++11提供的另外一种用于等待的同步机制,它能阻塞一个或多个线程,直到收到另外一个线程发出的通知或者超时......
  • 【C++BFS算法】752 打开转盘锁
    本文涉及知识点C++BFS算法LeetCode752打开转盘锁你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字:‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’。每个拨轮可以自由旋转:例如把‘9’变为‘0’,‘0’变为‘9’。每次旋转都只能旋......
  • 【NOI】C++数据结构入门之一维数组(一)数组基础
    文章目录前言一、概念1.导入2.数组2.1数组的创建2.2数组的使用二、例题讲解问题:1423-考试成绩的简单统计问题:1153-查找“支撑数”问题:1156-排除异形基因问题:1155-找找谁的身高超过全家的平均身高问题:1231-考试成绩的分布情况三、总结四、感谢前言在......
  • C++11标准库 互斥锁 <mutex> 梳理
    目录<mutex>std::call_once函数例程:使用call_once实现的单例模式std::mutex类--独占互斥锁成员函数std::recursive_mutex类--递归互斥锁使用注意:描述:std::timed_mutex类--超时互斥锁描述:成员函数:std::recursive_timed_mutex类std::lock_guard模板类函数原型:std::uniqu......
  • 【C++11常见新特性(三)】线程库
    文章目录thread类线程函数参数并行与并发的区别原子性操作库关于atomic类模板对比锁和原子操作lock_guard与unique_locklock_guardunique_lock两个线程交替打印奇数和偶数thread类C++11新特性支持线程,使得C++在并行编程中不需要使用第三方库,并且在原子操作中还引......
  • C++11 标准库 线程库<thread>梳理
    目录<thread>this_thread命名空间1.get_id()2.sleep_for()3.sleep_until()4.yield()thread类构造函数:类方法1.get_id()2.join()3.detach()4.joinable()5.operator=6.hardware_concurrency(static)多线程的两种计算场景<thread>this_thread命名空间在C++11中不仅添加......
  • C++11时间工具<chrono>梳理
    目录<chrono>时间间隔duration常用的duration时间点time_point时钟system_clock&steady_clocksystem_clock代码举例steady_clock例程:转换函数1.duration_castDescription:duration支持隐式转换的规则2.time_point_cast<chrono>C++11中提供了日期和时间相关的库chrono。chro......