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(); </…...

鼠害监测系统:科技守护农业安全
在农业生产中,鼠害一直是威胁作物安全、影响产量的重要因素。然而,随着科技的飞速发展,鼠害监测系统正逐步成为现代农业防治鼠害的重要利器。 鼠害监测系统巧妙融合了现代光电、数控及物联网技术,实现了诱鼠、投喂鼠药、鼠情监测及…...

python打卡day49
知识点回顾: 通道注意力模块复习空间注意力模块CBAM的定义 作业:尝试对今天的模型检查参数数目,并用tensorboard查看训练过程 import torch import torch.nn as nn# 定义通道注意力 class ChannelAttention(nn.Module):def __init__(self,…...
C++:std::is_convertible
C++标志库中提供is_convertible,可以测试一种类型是否可以转换为另一只类型: template <class From, class To> struct is_convertible; 使用举例: #include <iostream> #include <string>using namespace std;struct A { }; struct B : A { };int main…...

练习(含atoi的模拟实现,自定义类型等练习)
一、结构体大小的计算及位段 (结构体大小计算及位段 详解请看:自定义类型:结构体进阶-CSDN博客) 1.在32位系统环境,编译选项为4字节对齐,那么sizeof(A)和sizeof(B)是多少? #pragma pack(4)st…...
Python爬虫实战:研究feedparser库相关技术
1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...
spring:实例工厂方法获取bean
spring处理使用静态工厂方法获取bean实例,也可以通过实例工厂方法获取bean实例。 实例工厂方法步骤如下: 定义实例工厂类(Java代码),定义实例工厂(xml),定义调用实例工厂ÿ…...
【python异步多线程】异步多线程爬虫代码示例
claude生成的python多线程、异步代码示例,模拟20个网页的爬取,每个网页假设要0.5-2秒完成。 代码 Python多线程爬虫教程 核心概念 多线程:允许程序同时执行多个任务,提高IO密集型任务(如网络请求)的效率…...

IT供电系统绝缘监测及故障定位解决方案
随着新能源的快速发展,光伏电站、储能系统及充电设备已广泛应用于现代能源网络。在光伏领域,IT供电系统凭借其持续供电性好、安全性高等优势成为光伏首选,但在长期运行中,例如老化、潮湿、隐裂、机械损伤等问题会影响光伏板绝缘层…...
Spring AI与Spring Modulith核心技术解析
Spring AI核心架构解析 Spring AI(https://spring.io/projects/spring-ai)作为Spring生态中的AI集成框架,其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似,但特别为多语…...
在Ubuntu24上采用Wine打开SourceInsight
1. 安装wine sudo apt install wine 2. 安装32位库支持,SourceInsight是32位程序 sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine32:i386 3. 验证安装 wine --version 4. 安装必要的字体和库(解决显示问题) sudo apt install fonts-wqy…...

基于TurtleBot3在Gazebo地图实现机器人远程控制
1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...