algorithm算法库学习之——堆操作,最小/最大操作,比较操作,排列操作

embedded/2024/9/25 21:20:24/

algorithm此头文件是算法库的一部分。本篇介绍堆操作,最小/最大操作,比较操作,排列操作。

接口API

堆操作

is_heap

检查给定范围是否为一个最大堆
(函数模板)

is_heap_until

(C++11)

查找能成为最大堆的最大子范围
(函数模板)

make_heap

从一个元素范围创建出一个最大堆
(函数模板)

push_heap

将一个元素加入到一个最大堆
(函数模板)

pop_heap

从最大堆中移除最大元素
(函数模板)

sort_heap

将一个最大堆变成一个按升序排序的元素范围
(函数模板)
最小/最大操作

max

返回各给定值中的较大者
(函数模板)

max_element

返回范围内的最大元素
(函数模板)

min

返回各给定值中的较小者
(函数模板)

min_element

返回范围内的最小元素
(函数模板)

minmax

(C++11)

返回两个元素的较小和较大者
(函数模板)

minmax_element

(C++11)

返回范围内的最小元素和最大元素
(函数模板)
比较操作

equal

确定两个元素集合是否是相同的
(函数模板)

lexicographical_compare

当一个范围按字典顺序小于另一个范围时,返回 true
(函数模板)
排列操作

is_permutation

(C++11)

判断一个序列是否为另一个序列的排列
(函数模板)

next_permutation

产生某个元素范围的按字典顺序的下一个较大的排列
(函数模板)

prev_permutation

产生某个元素范围的按字典顺序的下一个较小的排列
(函数模板)

示例代码:

#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_clock#include <iomanip>bool myfn(int i, int j) { return i < j; }struct myclass {bool operator() (int i, int j) { return i < j; }
} myobj;bool is_palindrome(const std::string& s)
{return std::equal(s.cbegin(), s.cbegin() + s.size() / 2, s.crbegin());
}void test_equal(const std::string& s)
{std::cout << std::quoted(s)<< (is_palindrome(s) ? " 是" : " 不是")<< "回文\n";
}// a case-insensitive comparison function:
bool mycomp(char c1, char c2)
{return std::tolower(c1) < std::tolower(c2);
}template<typename Os, typename V>
Os& operator<<(Os& os, const V& v)
{os << "{ ";for (const auto& e : v)os << e << ' ';return os << '}';
}int main()
{// make_heap example 从一个元素范围创建出一个最大堆 int myints[] = { 10,20,30,5,15 };std::vector<int> v(myints, myints + 5);std::make_heap(v.begin(), v.end());std::cout << "initial max heap   : " << v.front() << '\n';// pop_heap example 从最大堆中移除最大元素std::pop_heap(v.begin(), v.end()); v.pop_back();std::cout << "max heap after pop : " << v.front() << '\n';// push_heap example 将一个元素加入到一个最大堆 v.push_back(99); std::push_heap(v.begin(), v.end());std::cout << "max heap after push: " << v.front() << '\n';// sort_heap example 将一个最大堆变成一个按升序排序的元素范围std::sort_heap(v.begin(), v.end());std::cout << "final sorted range :";for (unsigned i = 0; i < v.size(); i++)std::cout << ' ' << v[i];std::cout << '\n';// is_heap example 检查给定范围是否为一个最大堆 std::vector<int> foo{ 9,5,2,6,4,1,3,8,7 };if (!std::is_heap(foo.begin(), foo.end()))std::make_heap(foo.begin(), foo.end());std::cout << "Popping out elements:";while (!foo.empty()) {std::pop_heap(foo.begin(), foo.end());   // moves largest element to backstd::cout << ' ' << foo.back();         // prints backfoo.pop_back();                         // pops element out of container}std::cout << '\n';// is_heap_until example 查找能成为最大堆的最大子范围std::vector<int> foo2{ 2,6,9,3,8,4,5,1,7 };std::sort(foo2.begin(), foo2.end());std::reverse(foo2.begin(), foo2.end());auto last = std::is_heap_until(foo2.begin(), foo2.end());std::cout << "The " << (last - foo2.begin()) << " first elements are a valid heap:";for (auto it = foo2.begin(); it != last; ++it)std::cout << ' ' << *it;std::cout << '\n';// min example 返回各给定值中的较小者std::cout << "min(1,2)==" << std::min(1, 2) << '\t';std::cout << "min(2,1)==" << std::min(2, 1) << '\t';std::cout << "min('a','z')==" << std::min('a', 'z') << '\t';std::cout << "min(3.14,2.72)==" << std::min(3.14, 2.72) << '\n';// max example 返回各给定值中的较大者 std::cout << "max(1,2)==" << std::max(1, 2) << '\t';std::cout << "max(2,1)==" << std::max(2, 1) << '\t';std::cout << "max('a','z')==" << std::max('a', 'z') << '\t';std::cout << "max(3.14,2.73)==" << std::max(3.14, 2.73) << '\n';// minmax example 返回两个元素的较小和较大者 auto result = std::minmax({ 1,2,3,4,5 });std::cout << "minmax({1,2,3,4,5}): ";std::cout << result.first << ' ' << result.second << '\n';// min_element example 返回范围内的最小元素 // max_element example 返回范围内的最大元素 int myints2[] = { 3,7,2,5,6,4,9 };// using default comparison:std::cout << "The smallest element is " << *std::min_element(myints2, myints2 + 7) << '\n';std::cout << "The largest element is " << *std::max_element(myints2, myints2 + 7) << '\n';// using function myfn as comp:std::cout << "The smallest element is " << *std::min_element(myints2, myints2 + 7, myfn) << '\n';std::cout << "The largest element is " << *std::max_element(myints2, myints2 + 7, myfn) << '\n';// using object myobj as comp:std::cout << "The smallest element is " << *std::min_element(myints2, myints2 + 7, myobj) << '\n';std::cout << "The largest element is " << *std::max_element(myints2, myints2 + 7, myobj) << '\n';// minmax_element example 返回范围内的最小元素和最大元素 std::array<int, 7> foo3{ 3,7,2,9,5,8,6 };auto result3 = std::minmax_element(foo3.begin(), foo3.end());// print result:std::cout << "min is " << *result3.first;std::cout << ", at position " << (result3.first - foo3.begin()) << '\n';std::cout << "max is " << *result3.second;std::cout << ", at position " << (result3.second - foo3.begin()) << '\n';// equal example 确定两个元素集合是否是相同的 test_equal("radar");test_equal("hello");// lexicographical_compare example 当一个范围按字典顺序小于另一个范围时,返回 truechar foo5[] = "Apple";char bar5[] = "apartment";std::cout << std::boolalpha;std::cout << "Comparing foo and bar lexicographically (foo<bar):\n";std::cout << "Using default comparison (operator<): ";std::cout << std::lexicographical_compare(foo5, foo5 + 5, bar5, bar5 + 9);std::cout << '\n';std::cout << "Using mycomp as comparison object: ";std::cout << std::lexicographical_compare(foo5, foo5 + 5, bar5, bar5 + 9, mycomp);std::cout << '\n';// is_permutation example 判断一个序列是否为另一个序列的排列 static auto v1 = { 1, 2, 3, 4, 5 };static auto v2 = { 3, 5, 4, 1, 2 };static auto v3 = { 3, 5, 4, 1, 1 };std::cout << v2 << " 是 " << v1 << " 的排列:" << std::boolalpha<< std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n'<< v3 << " 是 " << v1 << " 的排列:" << std::boolalpha<< std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n';// next_permutation example 产生某个元素范围的按字典顺序的下一个较大的排列 int myints6[] = { 1,2,3 };std::sort(myints6, myints6 + 3);std::cout << "The 3! possible permutations with 3 elements:\n";do {std::cout << myints6[0] << ' ' << myints6[1] << ' ' << myints6[2] << '\n';} while (std::next_permutation(myints6, myints6 + 3));std::cout << "After loop: " << myints6[0] << ' ' << myints6[1] << ' ' << myints6[2] << '\n';// prev_permutation example 产生某个元素范围的按字典顺序的下一个较小的排列 int myints7[] = { 1,2,3 };std::sort(myints7, myints7 + 3);std::reverse(myints7, myints7 + 3);std::cout << "The 3! possible permutations with 3 elements:\n";do {std::cout << myints7[0] << ' ' << myints7[1] << ' ' << myints7[2] << '\n';} while (std::prev_permutation(myints7, myints7 + 3));std::cout << "After loop: " << myints7[0] << ' ' << myints7[1] << ' ' << myints7[2] << '\n';std::cout << "hello world\n";return 0;
}

运行结果:

参考:

https://cplusplus.com/reference/algorithm/

标准库标头 <algorithm> - cppreference.com


http://www.ppmy.cn/embedded/98219.html

相关文章

计算机毕业设计选题推荐-软科大学排名可视化分析-Python爬虫

✨作者主页&#xff1a;IT研究室✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Python…

【Gradle】代理配置

【Gradle】代理配置 背景简介开发环境开发步骤及源码 背景 随着年龄的增长&#xff0c;很多曾经烂熟于心的技术原理已被岁月摩擦得愈发模糊起来&#xff0c;技术出身的人总是很难放下一些执念&#xff0c;遂将这些知识整理成文&#xff0c;以纪念曾经努力学习奋斗的日子。本文…

使用微软Detours库进行模块枚举

Detours 是微软开发的一个强大的 Windows API 钩子库&#xff0c;用于监视和拦截函数调用。它广泛应用于微软产品团队和众多独立软件开发中&#xff0c;旨在无需修改原始代码的情况下实现函数拦截和修改。Detours 在调试、监控、日志记录和性能分析等方面表现出色&#xff0c;已…

特洛伊木马:现代网络安全的隐形威胁

在网络安全领域&#xff0c;特洛伊木马&#xff08;Trojan Horse&#xff0c;简称木马&#xff09;是一种古老却依然十分有效的攻击手段。尽管名字来源于古希腊神话中的特洛伊战争&#xff0c;但在现代信息技术中&#xff0c;木马病毒已演变为一种极具破坏性和隐蔽性的恶意软件…

Java学习Day27:Mysql第一章:陨落的天才

陨落天才之猛猛奋起&#xff01; 1. MySQL 基本指令 显示数据库 show datadases; 创建数据库 create database 数据库名; 选择数据库 ues 数据库名&#xff1a; 查看数据库中存在的表 show tables; #要求前面有use语句 创建表 create table 表名(字段名1 数据类型&am…

对公金融业务产品系统应用架构设计(三)

中国贸易金融跨行交易区块链平台2019年12月,中国银保监会、商务部和外汇局联合印发了《关于完善外贸金融服务的指导意见》(银保监发〔2019〕49号),其中明确要求“银行业和保险业自律组织应加强调研,深入了解银行保险机构外贸金融业务诉求,在推动完善法律法规、规范展业标…

跟着 iLogtail 学习高质量软件建设

作者&#xff1a;余韬 本文根据 iLogtail PMC 成员余韬 2024 年 6 月 26 日在 DBAPlus 社群的公开直播《云上千万级可观测 Agent SRE 实践》整理而成。 引言 近年来&#xff0c;关于可靠性工程这一话题的热议不断升温&#xff0c;这主要归因于当前形势的显著变化。 首先&…

0.91寸OLED迷你音频频谱

一、简介 音频频谱在最小0.91寸OLED 屏幕上显示&#xff0c;小巧玲珑 二、应用场景 本模块为音频频谱显示模块&#xff0c;用来获取声音频谱并展示频谱&#xff0c;跟随音乐声音律动 三、产品概述 基于主控芯片设计的将声音采集分析频谱&#xff0c;显示到0.91寸OLED的功能…