algorithm算法库学习之——划分操作和排序操作
algorithm此头文件是算法库的一部分。本篇介绍划分操作和排序操作。
划分操作 | |
| is_partitioned (C++11) | 判断范围是否已按给定的谓词划分 (函数模板) |
| partition | 将范围中的元素分为两组 (函数模板) |
| partition_copy (C++11) | 复制一个范围,将各元素分为两组 (函数模板) |
| stable_partition | 将元素分为两组,同时保留其相对顺序 (函数模板) |
| partition_point (C++11) | 定位已划分范围的划分点 (函数模板) |
排序操作 | |
| is_sorted (C++11) | 检查范围是否已按升序排列 (函数模板) |
| is_sorted_until (C++11) | 找出最大的有序子范围 (函数模板) |
| sort | 将范围按升序排序 (函数模板) |
| partial_sort | 排序一个范围的前 N 个元素 (函数模板) |
| partial_sort_copy | 对范围内的元素进行复制并部分排序 (函数模板) |
| stable_sort | 将范围内的元素排序,同时保持相等的元素之间的顺序 (函数模板) |
| nth_element | 将给定的范围部分排序,确保其按给定元素划分 |
示例代码:
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <utility> // std::pair
#include <vector>
#include <array>
#include <cctype> // std::tolower
#include <string>
#include <ctime> // std::time
#include <cstdlib> // std::rand, std::srand
#include <random> // std::default_random_engine
#include <chrono> // std::chrono::system_clockbool IsOdd5(int i) { return ((i % 2) == 1); }bool myfunction7(int i, int j) { return (i < j); }struct myclass10{bool operator() (int i, int j) { return (i < j); }
} myobject10;bool compare_as_ints(double i, double j)
{return (int(i) < int(j));
}int main()
{// is_partitioned algorithm examplestd::array<int, 7> foo{ 1,2,3,4,5,6,7 };// print contents:std::cout << "foo:"; for (int& x : foo) std::cout << ' ' << x;if (std::is_partitioned(foo.begin(), foo.end(), IsOdd5))std::cout << " (partitioned)\n";elsestd::cout << " (not partitioned)\n";// partition array:std::partition(foo.begin(), foo.end(), IsOdd5);// print contents again:std::cout << "foo:"; for (int& x : foo) std::cout << ' ' << x;if (std::is_partitioned(foo.begin(), foo.end(), IsOdd5))std::cout << " (partitioned)\n";elsestd::cout << " (not partitioned)\n";// partition algorithm examplestd::vector<int> myvector;// set some values:for (int i = 1; i < 10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9std::vector<int>::iterator bound;bound = std::partition(myvector.begin(), myvector.end(), IsOdd5);// print out content:std::cout << "odd elements:";for (std::vector<int>::iterator it = myvector.begin(); it != bound; ++it)std::cout << ' ' << *it;std::cout << '\n';std::cout << "even elements:";for (std::vector<int>::iterator it = bound; it != myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';// stable_partition examplestd::vector<int> myvector2;// set some values:for (int i = 1; i < 10; ++i) myvector2.push_back(i); // 1 2 3 4 5 6 7 8 9std::vector<int>::iterator bound2;bound2 = std::stable_partition(myvector2.begin(), myvector2.end(), IsOdd5);// print out content:std::cout << "odd elements:";for (std::vector<int>::iterator it = myvector2.begin(); it != bound2; ++it)std::cout << ' ' << *it;std::cout << '\n';std::cout << "even elements:";for (std::vector<int>::iterator it = bound2; it != myvector2.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';// partition_copy examplestd::vector<int> foo2{ 1,2,3,4,5,6,7,8,9 };std::vector<int> odd2, even2;// resize vectors to proper size:unsigned n = std::count_if(foo2.begin(), foo2.end(), IsOdd5);odd2.resize(n); even2.resize(foo2.size() - n);// partition:std::partition_copy(foo2.begin(), foo2.end(), odd2.begin(), even2.begin(), IsOdd5);// print contents:std::cout << "odd2: "; for (int& x : odd2) std::cout << ' ' << x; std::cout << '\n';std::cout << "even2: "; for (int& x : even2) std::cout << ' ' << x; std::cout << '\n';// partition_point examplestd::vector<int> foo3{ 1,2,3,4,5,6,7,8,9 };std::vector<int> odd3;std::partition(foo3.begin(), foo3.end(), IsOdd5);auto it = std::partition_point(foo3.begin(), foo3.end(), IsOdd5);odd3.assign(foo3.begin(), it);// print contents of odd3:std::cout << "odd3:";for (int& x : odd3) std::cout << ' ' << x;std::cout << '\n';// sort algorithm exampleint myints4[] = { 32,71,12,45,26,80,53,33 };std::vector<int> myvector4(myints4, myints4 + 8); // 32 71 12 45 26 80 53 33// using default comparison (operator <):std::sort(myvector4.begin(), myvector4.begin() + 4); //(12 32 45 71)26 80 53 33// using function as compstd::sort(myvector4.begin() + 4, myvector4.end(), myfunction7); // 12 32 45 71(26 33 53 80)// using object as compstd::sort(myvector4.begin(), myvector4.end(), myobject10); //(12 26 32 33 45 53 71 80)// print out content:std::cout << "myvector4 contains:";for (std::vector<int>::iterator it = myvector4.begin(); it != myvector4.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';// stable_sort exampledouble mydoubles[] = { 3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58 };std::vector<double> myvector5;myvector5.assign(mydoubles, mydoubles + 8);std::cout << "using default comparison:";std::stable_sort(myvector5.begin(), myvector5.end());for (std::vector<double>::iterator it = myvector5.begin(); it != myvector5.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';myvector5.assign(mydoubles, mydoubles + 8);std::cout << "using 'compare_as_ints' :";std::stable_sort(myvector5.begin(), myvector5.end(), compare_as_ints);for (std::vector<double>::iterator it = myvector5.begin(); it != myvector5.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';// partial_sort exampleint myints6[] = { 9,8,7,6,5,4,3,2,1 };std::vector<int> myvector6(myints6, myints6 + 9);// using default comparison (operator <):std::partial_sort(myvector6.begin(), myvector6.begin() + 5, myvector6.end());// using function as compstd::partial_sort(myvector6.begin(), myvector6.begin() + 5, myvector6.end(), myfunction7);// print out content:std::cout << "myvector6 contains:";for (std::vector<int>::iterator it = myvector6.begin(); it != myvector6.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';// partial_sort_copy exampleint myints7[] = { 9,8,7,6,5,4,3,2,1 };std::vector<int> myvector7(5);// using default comparison (operator <):std::partial_sort_copy(myints7, myints7 + 9, myvector7.begin(), myvector7.end());// using function as compstd::partial_sort_copy(myints7, myints7 + 9, myvector7.begin(), myvector7.end(), myfunction7);// print out content:std::cout << "myvector7 contains:";for (std::vector<int>::iterator it = myvector7.begin(); it != myvector7.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';// is_sorted examplestd::array<int, 4> foo4{ 2,4,1,3 };do {// try a new permutation:std::prev_permutation(foo4.begin(), foo4.end());// print range:std::cout << "foo4:";for (int& x : foo4) std::cout << ' ' << x;std::cout << '\n';} while (!std::is_sorted(foo4.begin(), foo4.end()));std::cout << "the range is sorted!\n";// is_sorted_until examplestd::array<int, 4> foo5{ 2,4,1,3 };std::array<int, 4>::iterator it5;do {// try a new permutation:std::prev_permutation(foo5.begin(), foo5.end());// print range:std::cout << "foo5:";for (int& x : foo5) std::cout << ' ' << x;it5 = std::is_sorted_until(foo5.begin(), foo5.end());std::cout << " (" << (it5 - foo5.begin()) << " elements sorted)\n";} while (it5 != foo5.end());std::cout << "the range is sorted!\n";// nth_element examplestd::vector<int> myvector8;// set some values:for (int i = 1; i < 10; i++) myvector8.push_back(i); // 1 2 3 4 5 6 7 8 9std::random_shuffle(myvector8.begin(), myvector8.end());// using default comparison (operator <):std::nth_element(myvector8.begin(), myvector8.begin() + 5, myvector8.end());// using function as compstd::nth_element(myvector8.begin(), myvector8.begin() + 5, myvector8.end(), myfunction7);// print out content:std::cout << "myvector8 contains:";for (std::vector<int>::iterator it = myvector8.begin(); it != myvector8.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}
运行结果:

参考:
https://cplusplus.com/reference/algorithm/
标准库标头 <algorithm> - cppreference.com
相关文章:
algorithm算法库学习之——划分操作和排序操作
algorithm此头文件是算法库的一部分。本篇介绍划分操作和排序操作。 划分操作 is_partitioned (C11) 判断范围是否已按给定的谓词划分 (函数模板) partition 将范围中的元素分为两组 (函数模板) partition_copy (C11) 复制一个范围,将各元素分为两组 (函数模板) st…...
XSS实验记录
目录 XXS地址 实验过程 Ma Spaghet Jeff Ugandan Knuckles Ricardo Milos Ah Thats Hawt Ligma Mafia Ok, Boomer XXS地址 XSS Game - Learning XSS Made Simple! | Created by PwnFunction 实验过程 Ma Spaghet 要求我们弹出一个alert(1337)sandbox.pwnfuncti…...
Cortex-A7的GIC(全局中断控制器)使用方法(7):基于stm32MP135的GIC配置中断效果测试
0 参考资料 STM32MP13xx参考手册.pdf(RM0475) ARM Generic Interrupt Controller Architecture version 2.0 - Architecture Specification.pdf 1 GIC配置中断效果测试 前面我们已经实现了GIC的配置,为了验证GIC是否配置有效,本例…...
c++动态数组new和delete
文章目录 动态数组的使用大全1. **基本创建和初始化**2. **动态调整大小**3. **动态数组的使用与标准库 std::vector**4. **动态数组作为函数参数**输出 5. **使用动态数组存储用户输入** 动态数组的使用大全 1. 基本创建和初始化 示例: #include <iostream&g…...
Redis热点知识速览(redis的数据结构、高性能、持久化、主从复制、集群、缓存淘汰策略、事务、Pub/Sub、锁机制、常见问题等)
Redis是一个开源的、使用内存作为存储的、支持数据结构丰富的NoSQL数据库。它的高性能、灵活性和简单易用使其在许多场景下成为首选的缓存解决方案。以下是Redis的常见和热点知识总结。 数据结构 Redis支持五种基本数据结构: String:字符串是Redis中最…...
【C++浅析】lambda表达式:基本结构 使用示例
基本结构 [捕获列表](参数列表) -> 返回类型 { // 函数体 } 捕获列表 ([ ]): 用于指定外部变量的捕获方式。可以: 通过值捕获:[x]通过引用捕获:[&x]捕获所有变量通过值:[]捕获所有变量通过引用:[&]自…...
利用Redis获取权限的多种方式
更多实战内容,可前往无问社区查看http://www.wwlib.cn/index.php/artread/artid/10333.html Redis是我们在实战中经常接触到的一款数据库,因其在前期打点中被利用后可直接影响服务器安全所以在攻防过程中也备受红队关注,在本文中会重点分享一…...
LeetCode - LCR 146- 螺旋遍历二维数组
LCR 146题 题目描述: 给定一个二维数组 array,请返回「螺旋遍历」该数组的结果。 螺旋遍历:从左上角开始,按照 向右、向下、向左、向上 的顺序 依次 提取元素,然后再进入内部一层重复相同的步骤,直到提取完…...
如何获取Bing站长工具API密钥
Bing站长工具近期悄然上线了网站URL推送功能,似乎有意跟随百度的步伐。这个新功能允许站长通过API向Bing提交链接数据,当然也可以通过Bing站长工具手动提交。 本文将详细介绍如何通过Bing站长工具生成用于网站链接推送的API密钥。 首先,访问…...
NC 调整数组顺序使奇数位于偶数前面(一)
系列文章目录 文章目录 系列文章目录前言 前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站,这篇文章男女通用,看懂了就去分享给你的码吧。 描述 输入一个长度…...
Unity异步把图片数据从显存下载到内存(GPU->CPU)
Unity异步把图片数据从显存下载到内存(GPU->CPU) 1.c#核心代码 using System.Collections; using System.Collections.Generic; using Unity.Collections; using UnityEditor.PackageManager.Requests; using UnityEngine; using UnityEngine.Rende…...
【MySQL】C/C++连接MySQL客户端,MySQL函数接口认知,图形化界面进行连接
【MySQL】C/C引入MySQL客户端 安装mysqlclient库mysql接口介绍初始化mysql_init链接数据库mysql_real_connect下发mysql命令mysql_query获取出错信息mysql_error获取执行结果mysql_store_result获取结果行数mysql_num_rows获取结果列数mysql_num_fields判断结果列数mysql_field…...
Wireshark分析工具
简单用例 首先打开软件,左上角点文件,选中要分析的文件列表。 导入用tcpdump抓的包后进行分析,这里要输入过滤条件,对网络包进行一定的过滤处理。(这里172网段是阿里云的地址,用自己写的python2脚本对阿里…...
linux网络配置脚本
通过脚本,设置静态ip以及主机名 因为企业9的网络配置文件和企业7的不一样所以,我们以rhel9和rhel7为例 rhel7/centos7/openeuler #!/bin/bash cat > /etc/sysconfig/network-scripts/ifcfg-$1 << EOF DEVICE$1 ONBOOTyes BOOTPROTOnone IPAD…...
IT管理:我与IT的故事4
首先,宣布一个“坏消息”。最近Herry童鞋的办公邮箱似乎有些“抽抽”了,所以邮件出现了延迟、拒收、被拒收、甚至是石沉大海的现象。为了能够更好的和大家进行沟通,大家如果发邮件到我办公邮箱的时候,若不嫌麻烦,可以抄…...
短链接系统设计方案
背景 需要设计一个短链接系统,主要功能主要有如下几点: ToB: 输入一个长链接,转换成短链接。这个短链接有时效性,可以设定指定过期时间。这个系统的每天会生成千万级别的短链接。数据具备可分析功能。 ToC…...
Cisco交换机SSH使用RSA公钥免密登录(IOS与Nexus,服务器以RHEL8为例)
目录 需求实验步骤0. 实验环境1. Linux2. CiscoIOS基础设置保存密钥登陆测试 3. CiscoNexus基础配置保存密钥登陆测试 需求 在实际工作中,常会遇到自动化的需求,那么在自动采集、配置等对网络设备的自动化需求中,不可避免的会遇到需要登录-&…...
QT判断操作系统类型和CPU架构
一、判断操作系统类型 1.在.pro文件中判断 macx { # mac only } unix:!macx{ # linux only } win32 { # windows only }2.在代码中判断 可以包含QGlobal头文件,判断预定义宏 #include <QtGlobal> ... #ifdef Q_OS_MAC // mac #endif#ifdef Q_OS_LINUX // …...
input[type=checkbox]勾选框自定义样式
效果图: <template> <input class"rule-checkbox" type"checkbox" checked v-model"isChecked" /> </template><script setup lang"ts"> import { ref } from vue; const isChecked ref(); </…...
鼠害监测系统:科技守护农业安全
在农业生产中,鼠害一直是威胁作物安全、影响产量的重要因素。然而,随着科技的飞速发展,鼠害监测系统正逐步成为现代农业防治鼠害的重要利器。 鼠害监测系统巧妙融合了现代光电、数控及物联网技术,实现了诱鼠、投喂鼠药、鼠情监测及…...
SpringBoot-17-MyBatis动态SQL标签之常用标签
文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…...
云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?
大家好,欢迎来到《云原生核心技术》系列的第七篇! 在上一篇,我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在,我们就像一个拥有了一块崭新数字土地的农场主,是时…...
SkyWalking 10.2.0 SWCK 配置过程
SkyWalking 10.2.0 & SWCK 配置过程 skywalking oap-server & ui 使用Docker安装在K8S集群以外,K8S集群中的微服务使用initContainer按命名空间将skywalking-java-agent注入到业务容器中。 SWCK有整套的解决方案,全安装在K8S群集中。 具体可参…...
椭圆曲线密码学(ECC)
一、ECC算法概述 椭圆曲线密码学(Elliptic Curve Cryptography)是基于椭圆曲线数学理论的公钥密码系统,由Neal Koblitz和Victor Miller在1985年独立提出。相比RSA,ECC在相同安全强度下密钥更短(256位ECC ≈ 3072位RSA…...
vscode(仍待补充)
写于2025 6.9 主包将加入vscode这个更权威的圈子 vscode的基本使用 侧边栏 vscode还能连接ssh? debug时使用的launch文件 1.task.json {"tasks": [{"type": "cppbuild","label": "C/C: gcc.exe 生成活动文件"…...
【磁盘】每天掌握一个Linux命令 - iostat
目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat(I/O Statistics)是Linux系统下用于监视系统输入输出设备和CPU使…...
如何将联系人从 iPhone 转移到 Android
从 iPhone 换到 Android 手机时,你可能需要保留重要的数据,例如通讯录。好在,将通讯录从 iPhone 转移到 Android 手机非常简单,你可以从本文中学习 6 种可靠的方法,确保随时保持连接,不错过任何信息。 第 1…...
Java入门学习详细版(一)
大家好,Java 学习是一个系统学习的过程,核心原则就是“理论 实践 坚持”,并且需循序渐进,不可过于着急,本篇文章推出的这份详细入门学习资料将带大家从零基础开始,逐步掌握 Java 的核心概念和编程技能。 …...
Android第十三次面试总结(四大 组件基础)
Activity生命周期和四大启动模式详解 一、Activity 生命周期 Activity 的生命周期由一系列回调方法组成,用于管理其创建、可见性、焦点和销毁过程。以下是核心方法及其调用时机: onCreate() 调用时机:Activity 首次创建时调用。…...
云原生玩法三问:构建自定义开发环境
云原生玩法三问:构建自定义开发环境 引言 临时运维一个古董项目,无文档,无环境,无交接人,俗称三无。 运行设备的环境老,本地环境版本高,ssh不过去。正好最近对 腾讯出品的云原生 cnb 感兴趣&…...
