当前位置: 首页 > news >正文

STL——常用算法(二)

一、常用拷贝和替换算法

1.copy

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}vector<int>v2;v2.resize(v1.size());copy(v1.begin(), v1.end(), v2.begin());for_each(v2.begin(), v2.end(), printVector);cout << endl;
}
int main()
{test01();return 0;
}

2.replace

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}replace(v1.begin(), v1.end(), 5, 10);for_each(v1.begin(), v1.end(), printVector);cout << endl;
}
int main()
{test01();return 0;
}

3.replace_if

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
class Greater5
{
public:bool operator()(int val){return val >= 5;}
};
void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}for_each(v1.begin(), v1.end(), MyPrint());cout << endl;replace_if(v1.begin(), v1.end(), Greater5(), 10);for_each(v1.begin(), v1.end(), MyPrint());cout << endl;
}
int main()
{test01();return 0;
}

4.swap

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 100);}for_each(v1.begin(), v1.end(), MyPrint());cout << endl;for_each(v2.begin(), v2.end(), MyPrint());cout << endl;swap(v1, v2);for_each(v1.begin(), v1.end(), MyPrint());cout << endl;for_each(v2.begin(), v2.end(), MyPrint());cout << endl;
}
int main()
{test01();return 0;
}

二、常用算数生成算法

1.accumulate

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i <= 100; i++){v1.push_back(i);}int total = accumulate(v1.begin(), v1.end(), 1000);//5050+1000//for_each(v1.begin(), v1.end(), MyPrint());cout << total << endl;
}
int main()
{test01();return 0;
}

2.fill

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v1;v1.resize(10);for_each(v1.begin(), v1.end(), MyPrint());cout << endl;fill(v1.begin(), v1.end(), 10);for_each(v1.begin(), v1.end(), MyPrint());
}
int main()
{test01();return 0;
}

三、常用集合算法

1.set_intersection

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>v3;v3.resize(min(v1.size(), v2.size()));vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), itEnd, MyPrint);cout << endl;
}
int main()
{test01();return 0;
}

2.set_union

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>v3;v3.resize(v1.size() + v2.size());vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), itEnd, MyPrint);cout << endl;
}
int main()
{test01();return 0;
}

3.set_difference

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>v3;v3.resize(max(v1.size(), v2.size()));vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());for_each(v3.begin(), itEnd, MyPrint);cout << endl;vector<int>::iterator itEnd1 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());for_each(v3.begin(), itEnd1, MyPrint);cout << endl;
}
int main()
{test01();return 0;
}

相关文章:

STL——常用算法(二)

一、常用拷贝和替换算法 1.copy #include <iostream> #include <vector> #include <algorithm> using namespace std; void printVector(int val) {cout << val << " "; } void test01() {vector<int>v1;for (int i 0; i <…...

MyCAT 2 底层原理

MyCAT 2 底层原理 1. MyCAT 2 架构概述 MyCAT 2 是一款开源的数据库中间件&#xff0c;它通过分库分表、读写分离、动态路由等机制提升数据库系统的性能和扩展性。MyCAT 2 的架构设计灵活&#xff0c;适用于多种数据库类型&#xff0c;包括 MySQL、PostgreSQL 和 SQL Server …...

操作系统实训复习笔记(第7关:生产者消费者问题实践)

目录 第7关&#xff1a;生产者消费者问题实践 第1关&#xff1a;生产者消费者问题实践 1、在主线程中初始化锁为解锁状态 2、访问对象时的加锁操作与解锁操作 3、&#xff08;生产和消费进程操作后&#xff09;信号量操作实现进程同步 4、先等待&#xff08;生产还是消费…...

通过物联网管理多台MQTT设备-基于全志T527开发板

一、系统概述 基于米尔-全志 T527设计一个简易的物联网网关&#xff0c;该网关能够管理多台MQTT设备&#xff0c;通过MQTT协议对设备进行读写操作&#xff0c;同时提供HTTP接口&#xff0c;允许用户通过HTTP协议与网关进行交互&#xff0c;并对设备进行读写操作。 二、系统架…...

Python学习前简介

1.python简介 2.python特点 3.python解释器 4.pyCharm简介 一、python简介 Python是一种高级编程语言&#xff0c;用于多种应用&#xff0c;包括网站开发、数据科学、人工智能、机器学习、桌面应用、网络应用、软件开发、网络爬虫等。它由Guido van Rossum于1991年首次发布&am…...

【Text2SQL 论文】MAGIC:为 Text2SQL 任务自动生成 self-correction guideline

论文&#xff1a;MAGIC: Generating Self-Correction Guideline for In-Context Text-to-SQL ⭐⭐⭐ 莱顿大学 & Microsoft, arXiv:2406.12692 一、论文速读 DIN-SQL 模型中使用了一个 self-correction 模块&#xff0c;他把 LLM 直接生成的 SQL 带上一些 guidelines 的 p…...

2024 年 8 款最佳建筑 3D 渲染软件

你现在使用的3D 渲染软件真得适合你吗&#xff1f; 在建筑和室内渲染当中&#xff0c;市面上有许多3D渲染软件可供选择。然而&#xff0c;并不是每款软件都适合你的需求。本指南将重点介绍2024年精选的8款最佳建筑3D渲染软件&#xff0c;帮助你了解不同的选项&#xff0c;并选…...

MAB规范(3):Chapter6 Glossary 术语表

第6章 - 术语表 此章不做过多的批注&#xff0c;都是些简单的术语解释。...

40python数据分析numpy基础之diag处理矩阵对角线元素

1 python数据分析numpy基础之diag处理矩阵对角线元素 python的numpy库的diag(v,k0)函数&#xff0c;以一维数组的形式返回方阵的对角线元素&#xff0c;或将一维数组转换为方阵&#xff08;非对角线元素为0&#xff09;。 方阵&#xff1a;方形矩阵&#xff0c;行数和列数相等…...

ffmpeg+nginx+video实现rtsp流转hls流,web页面播放

项目场景&#xff1a; 最近调试海康摄像头需要将rtsp流在html页面播放,因为不想去折腾推拉流&#xff0c;所以我选择ffmpeg转hls流&#xff0c;nginx转发&#xff0c;html直接访问就好了 1.首先要下载nginx和ffmpeg 附上下载地址&#xff1a; nginx nginx news ffmpeg htt…...

1、Redis系列-Redis高性能原理详解

Redis高性能原理详解 Redis是一款高性能的内存数据库&#xff0c;广泛应用于需要快速读写访问的数据密集型应用中。它的高性能得益于多方面的设计和优化。以下是Redis高性能实现的详细解释&#xff1a; 1. 单线程架构 Redis采用单线程架构来处理客户端请求&#xff0c;这与传…...

18.枚举

学习知识&#xff1a;枚举类型、相关的使用方法 Main.java&#xff1a; public class Main {public static void main(String[] args) {myenum[] colorlist myenum.values();//获取枚举中所有对象的引用数组for (myenum one : colorlist){System.out.println(one.toString(…...

全省高等职业学校大数据技术专业建设暨专业质量监测研讨活动顺利开展

6月21日&#xff0c;省教育评估院在四川邮电职业技术学院组织开展全省高等职业学校大数据技术专业建设暨专业质量监测研讨活动。省教育评估院副院长赖长春&#xff0c;四川邮电职业技术学院党委副书记、校长冯远洪&#xff0c;四川邮电职业技术学院党委委员、副校长程德杰等出席…...

2-16 基于matlab的动载荷简支梁模态分析程序

基于matlab的动载荷简支梁模态分析程序&#xff0c;可调节简支梁参数&#xff0c;包括截面宽、截面高、梁长度、截面惯性矩、弹性模量、密度。输出前四阶固有频率&#xff0c;任意时刻、位置的响应结果。程序已调通&#xff0c;可直接运行。 2-16 matlab 动载荷简支梁模态分析 …...

AI大模型的核心

前言 没错&#xff0c;AI大模型的核心成功因素通常可以归结为三大要素&#xff1a;大数据、大算力和强算法。这三个因素相辅相成&#xff0c;共同推动了现代人工智能技术的发展。 1. 大数据 • 定义&#xff1a;指的是涵盖广泛领域的海量数据&#xff0c;包括文本、图像、音…...

【Android面试八股文】ViewHolder为什么要被声明成静态内部类?

文章目录 ViewHolder为什么要被声明成静态内部类?1. 避免隐式引用导致的内存泄漏2. 提高性能3. 代码可读性和维护性实例代码总结ViewHolder为什么要被声明成静态内部类? 将 ViewHolder 声明为静态内部类有几个重要的原因,这样做可以提高性能并避免潜在的内存泄漏。下面是详…...

Android 11 系统OTA升级到旧版本(去除升级时间戳校验)

简介 由于客户要求能够通过OTA升级到旧版本因此探寻反向升级的方法。 方法一&#xff1a;进入recover模式 adb reboot recovery 点击Apply update from SD card 然后选择以前的OTA升级包就可以了。这种方式实测可以升级到旧的版本。但是我们的客户是通过在线升级软件进行更新…...

更新表的统计信息并清空缓存--DM8达梦数据库

更新表的统计信息并清空缓存--DM8达梦数据库 环境介绍1 收集 <表> 上所有对象信息&#xff0c;含索引2 清理缓存的执行计划3 达梦数据库学习使用列表 环境介绍 在某些环境刚完成数据迁移, 10万行以上大表数据量有修改1/3 ,查询条件已经创建索引,执行计划不好,或执行计划…...

【前后端实现】AHP权重计算

AHP权重计算&#xff1a; 需求&#xff1a;前端记录矩阵维度、上三角值&#xff0c;后端构建比较矩阵、计算权重值并将结果返回给前端 比较矩阵构建 如果你想要根据上三角&#xff08;不包括对角线&#xff09;的值来构建对称矩阵&#xff0c;那么你可以稍作修改上述的generate…...

K8S日常运维手册

Kubernetes&#xff08;简称 K8S&#xff09;是一种广泛使用的容器编排平台&#xff0c;能够自动化部署、扩展和管理容器化应用。对于运维人员来说&#xff0c;掌握 Kubernetes 的日常运维技能是确保系统稳定运行的关键。本文将介绍一些 Kubernetes 日常运维的基本操作与技巧&a…...

Vue3 + Element Plus + TypeScript中el-transfer穿梭框组件使用详解及示例

使用详解 Element Plus 的 el-transfer 组件是一个强大的穿梭框组件&#xff0c;常用于在两个集合之间进行数据转移&#xff0c;如权限分配、数据选择等场景。下面我将详细介绍其用法并提供一个完整示例。 核心特性与用法 基本属性 v-model&#xff1a;绑定右侧列表的值&…...

23-Oracle 23 ai 区块链表(Blockchain Table)

小伙伴有没有在金融强合规的领域中遇见&#xff0c;必须要保持数据不可变&#xff0c;管理员都无法修改和留痕的要求。比如医疗的电子病历中&#xff0c;影像检查检验结果不可篡改行的&#xff0c;药品追溯过程中数据只可插入无法删除的特性需求&#xff1b;登录日志、修改日志…...

《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- CSI-2 协议详细解析 (一)

CSI-2 协议详细解析 (一&#xff09; 1. CSI-2层定义&#xff08;CSI-2 Layer Definitions&#xff09; 分层结构 &#xff1a;CSI-2协议分为6层&#xff1a; 物理层&#xff08;PHY Layer&#xff09; &#xff1a; 定义电气特性、时钟机制和传输介质&#xff08;导线&#…...

java调用dll出现unsatisfiedLinkError以及JNA和JNI的区别

UnsatisfiedLinkError 在对接硬件设备中&#xff0c;我们会遇到使用 java 调用 dll文件 的情况&#xff0c;此时大概率出现UnsatisfiedLinkError链接错误&#xff0c;原因可能有如下几种 类名错误包名错误方法名参数错误使用 JNI 协议调用&#xff0c;结果 dll 未实现 JNI 协…...

MMaDA: Multimodal Large Diffusion Language Models

CODE &#xff1a; https://github.com/Gen-Verse/MMaDA Abstract 我们介绍了一种新型的多模态扩散基础模型MMaDA&#xff0c;它被设计用于在文本推理、多模态理解和文本到图像生成等不同领域实现卓越的性能。该方法的特点是三个关键创新:(i) MMaDA采用统一的扩散架构&#xf…...

【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例

文章目录 ★ position 的五种类型及基本用法 ★ 一、position 属性概述 二、position 的五种类型详解(初学者版) 1. static(默认值) 2. relative(相对定位) 3. absolute(绝对定位) 4. fixed(固定定位) 5. sticky(粘性定位) 三、定位元素的层级关系(z-i…...

Frozen-Flask :将 Flask 应用“冻结”为静态文件

Frozen-Flask 是一个用于将 Flask 应用“冻结”为静态文件的 Python 扩展。它的核心用途是&#xff1a;将一个 Flask Web 应用生成成纯静态 HTML 文件&#xff0c;从而可以部署到静态网站托管服务上&#xff0c;如 GitHub Pages、Netlify 或任何支持静态文件的网站服务器。 &am…...

视频字幕质量评估的大规模细粒度基准

大家读完觉得有帮助记得关注和点赞&#xff01;&#xff01;&#xff01; 摘要 视频字幕在文本到视频生成任务中起着至关重要的作用&#xff0c;因为它们的质量直接影响所生成视频的语义连贯性和视觉保真度。尽管大型视觉-语言模型&#xff08;VLMs&#xff09;在字幕生成方面…...

QT: `long long` 类型转换为 `QString` 2025.6.5

在 Qt 中&#xff0c;将 long long 类型转换为 QString 可以通过以下两种常用方法实现&#xff1a; 方法 1&#xff1a;使用 QString::number() 直接调用 QString 的静态方法 number()&#xff0c;将数值转换为字符串&#xff1a; long long value 1234567890123456789LL; …...

Java线上CPU飙高问题排查全指南

一、引言 在Java应用的线上运行环境中&#xff0c;CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时&#xff0c;通常会导致应用响应缓慢&#xff0c;甚至服务不可用&#xff0c;严重影响用户体验和业务运行。因此&#xff0c;掌握一套科学有效的CPU飙高问题排查方法&…...