C++相关闲碎记录(13)

news2024/10/5 14:14:49

1、排序算法

(1)对所有元素排序sort(), stable_sort()
#include "algostuff.hpp"

using namespace std;

int main() {
    deque<int> coll;
    INSERT_ELEMENTS(coll, 1, 9);
    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll, "on entry: ");
    sort(coll.begin(), coll.end());
    PRINT_ELEMENTS(coll, "sorted: ");
    sort(coll.begin(), coll.end(), greater<int>());
    PRINT_ELEMENTS(coll, "storted > ");
    return 0;
}
输出:
on entry: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 
sorted: 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
storted > 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1
#include "algostuff.hpp"

using namespace std;

bool lessLength(const string& s1, const string& s2) {
    return s1.length() < s2.length();
}
int main() {
    vector<string> coll1 = {"1xxxx", "2x", "3x", "4x", "5xx", "6xxxx", "7xx", "8xxx", "9xx", "10xxx","11", "12", "13", "14xx", "15","16","17"};
    vector<string> coll2(coll1);
    PRINT_ELEMENTS(coll1, "on entry: ");
    sort(coll1.begin(), coll1.end(), lessLength);
    stable_sort(coll2.begin(), coll2.end(), lessLength);
    PRINT_ELEMENTS(coll1, "with sort(): \n");
    PRINT_ELEMENTS(coll2, "with stable_sort(): \n");
    return 0;
}
输出:
on entry: 1xxxx 2x 3x 4x 5xx 6xxxx 7xx 8xxx 9xx 10xxx 11 12 13 14xx 15 16 17 
with sort():
2x 17 16 15 13 12 11 4x 3x 9xx 7xx 5xx 8xxx 14xx 10xxx 6xxxx 1xxxx
with stable_sort():
2x 3x 4x 11 12 13 15 16 17 5xx 7xx 9xx 8xxx 14xx 1xxxx 6xxxx 10xxx
(2)局部排序partial_sort(), partial_sort()
#include "algostuff.hpp"

using namespace std;

int main() {
    deque<int> coll;
    INSERT_ELEMENTS(coll, 3, 7);
    INSERT_ELEMENTS(coll, 2, 6);
    INSERT_ELEMENTS(coll, 1, 5);
    PRINT_ELEMENTS(coll);
    partial_sort(coll.begin(), coll.begin()+5,
                 coll.end());
    PRINT_ELEMENTS(coll);
    partial_sort(coll.begin(), coll.begin()+5,
                 coll.end(),
                 greater<int>());
    PRINT_ELEMENTS(coll);
    partial_sort(coll.begin(), coll.end(),
                 coll.end());
    PRINT_ELEMENTS(coll);
    return 0;
}
输出:
3 4 5 6 7 2 3 4 5 6 1 2 3 4 5 
1 2 2 3 3 7 6 5 5 6 4 4 3 4 5
7 6 6 5 5 1 2 2 3 3 4 4 3 4 5
1 2 2 3 3 3 4 4 4 5 5 5 6 6 7
(3)partial_sort_copy()
#include "algostuff.hpp"

using namespace std;

int main() {
    deque<int> coll1;
    vector<int> coll6(6);
    vector<int> coll30(30);
    INSERT_ELEMENTS(coll1, 3, 7);
    INSERT_ELEMENTS(coll1, 2, 6);
    INSERT_ELEMENTS(coll1, 1, 5);
    PRINT_ELEMENTS(coll1);
    vector<int>::const_iterator pos6;
    pos6 = partial_sort_copy(coll1.cbegin(), coll1.cend(),
                             coll6.begin(), coll6.end());
    copy(coll6.cbegin(), pos6,
         ostream_iterator<int>(cout, " "));
    cout << endl;
    vector<int>::const_iterator pos30;
    pos30 = partial_sort_copy(coll1.cbegin(), coll1.cend(),
                              coll30.begin(), coll30.end(),
                              greater<int>());
    copy(coll30.cbegin(), pos30,
         ostream_iterator<int>(cout, " "));
    cout << endl;
    return 0;
}
输出:
3 4 5 6 7 2 3 4 5 6 1 2 3 4 5 
1 2 2 3 3 3
7 6 6 5 5 5 4 4 4 3 3 3 2 2 1
(4)根据第n个元素排序nth_element()
#include "algostuff.hpp"

using namespace std;

int main() {
    deque<int> coll;
    INSERT_ELEMENTS(coll, 3, 7);
    INSERT_ELEMENTS(coll, 2, 6);
    INSERT_ELEMENTS(coll, 1, 5);
    PRINT_ELEMENTS(coll);
    
    nth_element(coll.begin(), coll.begin()+3, coll.end());
    cout << "the four lowest elements are: ";
    copy(coll.cbegin(), coll.cbegin()+4, ostream_iterator<int>(cout, " "));
    cout << endl;
    nth_element(coll.begin(), coll.end()-4, coll.end());
    cout << "the four highest elements are: ";
    copy(coll.cend()-4, coll.cend(), ostream_iterator<int>(cout, " "));
    cout << endl;
    nth_element(coll.begin(), coll.begin()+3,
                coll.end(), greater<int>());
    cout << "the four highest elements are: ";
    copy(coll.cbegin(), coll.cbegin()+4,
         ostream_iterator<int>(cout, " "));
    cout << endl;
    return 0;
}
输出:
3 4 5 6 7 2 3 4 5 6 1 2 3 4 5 
the four lowest elements are: 2 1 2 3
the four highest elements are: 5 6 6 7
the four highest elements are: 6 7 6 5
(5)make_heap()、push_heap()、pop_heap()、sort_heap()
#include "algostuff.hpp"

using namespace std;

int main() {
    vector<int> coll;
    INSERT_ELEMENTS(coll, 3, 7);
    INSERT_ELEMENTS(coll, 5, 9);
    INSERT_ELEMENTS(coll, 1, 4);
    PRINT_ELEMENTS(coll, "on entry: ");
    make_heap(coll.begin(), coll.end());
    PRINT_ELEMENTS(coll, "after make_heap(): ");
    pop_heap(coll.begin(), coll.end());
    coll.pop_back();
    PRINT_ELEMENTS(coll, "after pop_heap(): ");
    coll.push_back(17);
    push_heap(coll.begin(), coll.end());
    PRINT_ELEMENTS(coll, "after push_heap(): ");
    sort_heap(coll.begin(), coll.end());
    PRINT_ELEMENTS(coll, "after sort_heap(): ");
    
    return 0;
}
输出:
on entry: 3 4 5 6 7 5 6 7 8 9 1 2 3 4 
after make_heap(): 9 8 6 7 7 5 5 3 6 4 1 2 3 4
after pop_heap(): 8 7 6 7 4 5 5 3 6 4 1 2 3
after push_heap(): 17 7 8 7 4 5 6 3 6 4 1 2 3 5
after sort_heap(): 1 2 3 3 4 4 5 5 6 6 7 7 8 17

调用make_heap之后,元素被排序为9 8 6 7 7 5 5 3 6 4 1 2 3 4

 转换为二叉树如下:

 2、已排序区间算法

(1)binary_search()
#include "algostuff.hpp"

using namespace std;

int main() {
    list<int> coll;
    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll);
    if(binary_search(coll.begin(), coll.end(), 5)) {
        cout << "5 is present" << endl;
    } else {
        cout << "5 is not present" << endl;
    }
    if(binary_search(coll.begin(), coll.end(), 42)) {
        cout << "42 is present" << endl;
    } else {
        cout << "42 is not present" << endl;
    }
    return 0;
}
输出:
1 2 3 4 5 6 7 8 9 
5 is present
42 is not present
(2)检查数个元素是否存在includes()
#include "algostuff.hpp"

using namespace std;

int main() {
    list<int> coll;
    vector<int> search;
    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll, "coll: ");
    search.push_back(3);
    search.push_back(4);
    search.push_back(7);
    PRINT_ELEMENTS(search, "search: ");
    if(includes(coll.cbegin(), coll.cend(),
                search.cbegin(), search.cend())) {
        cout << "all elements of search are also in coll" << endl;
    } else {
        cout << "not all elements of search are also in coll" << endl;
    }
    return 0;
}
输出:
coll: 1 2 3 4 5 6 7 8 9 
search: 3 4 7
all elements of search are also in coll
(3)查找第一个或者最后一个可能的位置lower_bound()、upper_bound()
#include "algostuff.hpp"

using namespace std;

int main() {
    list<int> coll;
    INSERT_ELEMENTS(coll, 1, 9);
    INSERT_ELEMENTS(coll, 1, 9);
    coll.sort();
    PRINT_ELEMENTS(coll);
    auto pos1 = lower_bound(coll.cbegin(), coll.cend(), 5);
    auto pos2 = upper_bound(coll.cbegin(), coll.cend(), 5);
    cout << "5 could get position "
         << distance(coll.cbegin(), pos1) + 1
         << " up to " 
         << distance(coll.cbegin(), pos2) + 1
         << " without breaking the sorting" << endl;
    coll.insert(lower_bound(coll.begin(), coll.end(), 3), 3);
    coll.insert(upper_bound(coll.begin(), coll.end(), 7), 7);
    PRINT_ELEMENTS(coll);
    return 0;
}
输出:
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 
5 could get position 9 up to 11 without breaking the sorting
1 1 2 2 3 3 3 4 4 5 5 6 6 7 7 7 8 8 9 9
(4)查找第一个和最后一个可能位置equal_range()
#include "algostuff.hpp"

using namespace std;

int main() {
    list<int> coll;
    INSERT_ELEMENTS(coll, 1, 9);
    INSERT_ELEMENTS(coll, 1, 9);
    coll.sort();
    PRINT_ELEMENTS(coll);
    pair<list<int>::const_iterator, list<int>::const_iterator> range;
    range = equal_range(coll.cbegin(), coll.cend(), 5);
    cout << "5 could get position " 
         << distance(coll.cbegin(), range.first) + 1
         << " up to " 
         << distance(coll.cbegin(), range.second) + 1
         << " without breaking the sorting" << endl;
    return 0;
}
输出:
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 
5 could get position 9 up to 11 without breaking the sorting
(5)合并元素merge()
#include "algostuff.hpp"

using namespace std;

int main() {
    list<int> coll1;
    set<int> coll2;
    INSERT_ELEMENTS(coll1, 1, 6);
    INSERT_ELEMENTS(coll2, 3, 8);
    PRINT_ELEMENTS(coll1, "coll1: ");
    PRINT_ELEMENTS(coll2, "coll2: ");
    cout << "merged: ";
    merge(coll1.cbegin(), coll1.cend(),
          coll2.cbegin(), coll2.cend(),
          ostream_iterator<int>(cout, " "));
    cout << endl; 
    return 0;
}
输出:
coll1: 1 2 3 4 5 6 
coll2: 3 4 5 6 7 8
merged: 1 2 3 3 4 4 5 5 6 6 7 8
(6)两个已排序集合的并集set_union(),set_intersection(),set_difference(), set_symmetric_difference()
#include "algostuff.hpp"
using namespace std;

int main() {
    vector<int> c1 = {1, 2, 2, 4, 6, 7, 7, 9};
    deque<int> c2 = {2, 2, 2, 3, 6, 6, 8, 9};
    cout << "c1: ";
    copy(c1.cbegin(), c1.cend(), ostream_iterator<int>(cout, " "));
    cout << endl;
    cout << "c2: ";
    copy(c2.cbegin(), c2.cend(), ostream_iterator<int>(cout, " "));
    cout << endl << endl;
    cout << "merged: ";
    merge(c1.cbegin(), c1.cend(),
         c2.cbegin(), c2.cend(),
         ostream_iterator<int>(cout, " "));
    cout << endl;
    cout << "set_union: ";
    set_union(c1.cbegin(), c1.cend(),
              c2.cbegin(), c2.cend(),
              ostream_iterator<int>(cout, " "));
    cout << endl;
    cout << "set_intersection()";
    set_intersection(c1.cbegin(), c1.cend(),
                     c2.cbegin(), c2.cend(),
                     ostream_iterator<int>(cout, " "));
    cout << endl;
    cout << "set_difference()";
    set_difference(c1.cbegin(), c1.cend(),
                   c2.cbegin(), c2.cend(),
                   ostream_iterator<int>(cout, " "));
    cout << endl;
    cout << "set_symmetric_difference(): ";
    set_symmetric_difference(c1.cbegin(), c1.cend(),
                             c2.cbegin(), c2.cend(),
                             ostream_iterator<int>(cout, " "));
    cout << endl;
    return 0;
}
输出:
c1: 1 2 2 4 6 7 7 9 
c2: 2 2 2 3 6 6 8 9

merged: 1 2 2 2 2 2 3 4 6 6 6 7 7 8 9 9
set_union: 1 2 2 2 3 4 6 6 7 7 8 9
set_intersection()2 2 6 9
set_difference()1 4 7 7
set_symmetric_difference(): 1 2 3 4 6 7 7 8
(7)合并连贯之已排序区间inplace_merge()
#include "algostuff.hpp"

using namespace std;

int main() {
    list<int> coll;
    INSERT_ELEMENTS(coll, 1, 7);
    INSERT_ELEMENTS(coll, 1, 8);
    PRINT_ELEMENTS(coll);
    list<int>::iterator pos;
    pos = find(coll.begin(), coll.end(), 7);
    ++pos;
    inplace_merge(coll.begin(), pos, coll.end());
    PRINT_ELEMENTS(coll);
    return 0;
}
输出:
1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1313542.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

俄罗斯军方计划用 Astra Linux 取代 Windows!

网络安全正在改变全球化的面貌&#xff0c;各国政府为了防范外国的间谍和破坏活动&#xff0c;正积极发展自己的技术。在这一趋势下&#xff0c;俄罗斯军方已经开始用 Linux 发行版 Astra Linux 替换 Windows 系统。 如何提高Linux系统安全性&#xff1f;提升Linux安全的关键策…

紫禁城的雪花飞舞

在寂静的冬日&#xff0c;紫禁城迎来了它最美丽的时刻。一场突如其来的大雪&#xff0c;将故宫的每一砖每一瓦都覆盖在白色的雪被之下&#xff0c;仿佛将历史的痕迹都掩藏在一片纯净之中。 雪花纷纷扬扬&#xff0c;宛如历史的画卷在眼前徐徐展开。每一片雪花都像是从古人的诗…

设计原则 | 接口隔离原则

一、接口隔离原则 1、原理 客户端不应该依赖它不需要的接口&#xff0c;即一个类对另一个类的依赖应该建立在最小的接口上。如果强迫客户端依赖于那些它们不使用的接口&#xff0c;那么客户端就面临着这个未使用的接口的改变所带来的变更&#xff0c;这无意间导致了客户程序之…

高德地图+Vue中使用出现的问题

最近在做高德地图的逆向地理编码API出现了问题 按着官方的方式写代码运行时出现了问题&#xff0c;随后问了技术人员。 添加之后成功运行

Oracle数据库本地部署结合内网穿透实现公网环境PLSQL远程访问

文章目录 前言1. 数据库搭建2. 内网穿透2.1 安装cpolar内网穿透2.2 创建隧道映射 3. 公网远程访问4. 配置固定TCP端口地址4.1 保留一个固定的公网TCP端口地址4.2 配置固定公网TCP端口地址4.3 测试使用固定TCP端口地址远程Oracle 前言 Oracle&#xff0c;是甲骨文公司的一款关系…

基于电子密码锁具有掉电存储系统设计

**单片机设计介绍&#xff0c;基于电子密码锁具有掉电存储系统设计 文章目录 一 概要二、功能设计设计思路 三、 软件设计原理图 五、 程序六、 文章目录 一 概要 电子密码锁是一种使用电子技术实现开关门的装置&#xff0c;通常由密码输入板、电控锁、控制电路等组成。其中&a…

IPO:激光雷达迈向规模量产期,估值百亿的速腾聚创将如何通关?

智能驾驶大热&#xff0c;激光雷达为何“得宠”&#xff1f; 激光雷达作为智能驾驶汽车的“隐形安全气囊”&#xff0c;目前被业内普遍认为是实现高阶智能辅助驾驶&#xff08;城市NOA&#xff09;的捷径&#xff0c;其刚需性也越来越凸显。 因此&#xff0c;速腾聚创作为目前…

7. 异常、断言及日志

1.异常 1).什么是异常 异常&#xff0c;就是不正常的意思。指的是程序在执行过程中&#xff0c;出现的非正常的情况&#xff0c;最终会导致JVM的非正常停止。 在Java等面向对象的编程语言中&#xff0c;异常本身是一个类&#xff0c;产生异常就是创建异常对象并抛出了一个异常…

centos7安全防护_CPU占用率超过百分之300_centos7.4中毒CPU百分之百_清理毒源---Linux工作笔记068

执行top命令的时候看到有个进程: sshd占用cpu百分之300多...而且就算是kill -9 杀掉进程以后,进程又会自动启动 ll /proc/7298 我们执行这个命令,可以看到有个/var/tmp/sshd的文件 我们进入cd /var/tmp 然后我们执行 rm -rf sshd删除这个文件,然后我们再去top可以看到 cpu就…

十五、YARN辅助架构

1、学习内容 &#xff08;1&#xff09;了解什么是代理服务器 &#xff08;2&#xff09;了解什么是历史服务器 2、辅助架构 &#xff08;1&#xff09;辅助架构的由来 对于YARN架构来讲&#xff0c;除了ResourceManager集群资源总管家、NodeManager单机资源管家两个核心角…

轻量封装WebGPU渲染系统示例<44>- 材质组装流水线(MaterialPipeline)之灯光和阴影(源码)

目标: 数据化&#xff0c;模块化&#xff0c;自动化 备注: 从这个节点开始整体设计往系统规范的方向靠拢。之前的都算作是若干准备。所以会和之前的版本实现有些差异。 当前示例源码github地址: https://github.com/vilyLei/voxwebgpu/blob/feature/material/src/voxgpu/sa…

【Spring的AOP】Spring的简介、案例与工作流程

文章目录 1. 什么是AOP2. AOP的核心概念3. AOP的入门案例原始代码思路分析第一步&#xff1a;导入坐标第二步&#xff1a;制作连接点&#xff08;原始操作&#xff0c;Dao接口与实现类&#xff09;第三步&#xff1a;制作共性功能&#xff08;通知类与通知&#xff09;第四步&a…

用Excel绘制柱形图

在需要将数据用柱状图表示的时候&#xff0c;可以用Excel进行绘制。不单绘制柱形图&#xff0c;其他数据图也可以用Excel绘制。 接下来用绘制一个销售表的示例演示。 1.将数据输入Excel 数学书 语文书 英语书 一月 80 94 77 二月 95 86 84 三月 130 93 79 四月 …

深度学习环境配置

一、Anaconda安装 下载&#xff1a;从清华大学开源软件镜像下载 镜像网址 出现base即为安装成功&#xff1a; 检查显卡的驱动是否正确安装&#xff1a; &#xff08;GPU可以显示出名称&#xff09; GPU0是集显集成显卡是主板自带的显卡。 GPU1是独显即独立显卡&#xff0c…

C# 基本桌面编程(一)

前言 学习心得&#xff1a;C# 入门经典第8版书中的第14章《基本桌面编程》&#xff0c;文章的章节和部分介绍是引入书籍上的描述。如想多了解建议大家去购买书籍&#xff0c;进行阅读。 XAML XAML是一门使用XAMl语法的语言。XAML允许通过DirectX来使用这些显卡提供所有高级功能…

HPM6750系列--总章

本栏目介绍先楫半导体出品的HPM6750芯片&#xff08;基于HPM6750evkmini开发板&#xff09; ​​​​​​​ 内容概述 HPM6750系列--第一篇 初识HPM6750 介绍HPM6750芯片信息&#xff0c;包括主频、内存、外设配置&#xff0c;并列举了各种开发工具和开发资源。 HPM6750系列--…

101基于matlab的极限学习机ELM算法进行遥感图像分类

基于matlab的极限学习机ELM算法进行遥感图像分类&#xff0c;对所获取的遥感图片进行初步分类和最终分类。数据可更换自己的&#xff0c;程序已调通&#xff0c;可直接运行。 101matlab遥感图像分类模式识别 (xiaohongshu.com)

大数据生态圈kafka在物联网中的应用测试

背景 由物联网项目中使用到了Tbox应用管理车辆&#xff0c;在上报数据的过程中&#xff0c;需要将终端产生的数据通过kafka的produce topic customer对数据进行处理后&#xff0c;放置到mysql中。完成数据二进制到json转换工作。 Kafka的使用 查看kafka的topic ./kafka-topi…

【SpringBoot】从入门到精通的快速开发指南

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是Java方文山&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的专栏《SpringBoot》。&#x1f3af;&#x1f3af; &…

【开源软件】最好的开源软件-2023-第14名 Appsmith

自我介绍 做一个简单介绍&#xff0c;酒架年近48 &#xff0c;有20多年IT工作经历&#xff0c;目前在一家500强做企业架构&#xff0e;因为工作需要&#xff0c;另外也因为兴趣涉猎比较广&#xff0c;为了自己学习建立了三个博客&#xff0c;分别是【全球IT瞭望】&#xff0c;【…