STL常用算法-C++

概述:

  • 算法主要是由头文件 <algorithm> <functional> <numeric> 组成。
  • <algorithm> 是所有 STL 头文件中最大的一个,范围涉及是比较、交换、查找、遍历操作、复制、修改等等。
  • <functional> 定义了一些模板类,用以声明函数对象
  • <numeric> 体积很小,只包括几个在序列上面进行简单数学运算的模板函数

1. 常用的遍历算法

学习目标:

  • 掌握常用的遍历算法

算法简介:

  • for_each // 遍历容器
  • transform // 搬运容器到另一个容器中

1.1 for_each

功能描述:

  • 实现遍历容器

函数原型:

  • for_each(iterator beg, iterator end, _func);
    // 遍历算法 遍历容器元素
    // beg 开始迭代器
    // end 结束迭代器
    // _func 函数或者函数对象

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;//1.普通函数
void print01(int val) {cout << val << " ";
}//2.仿函数
class print02
{
public:void operator()(int val) {cout << val << " ";}
};int main() {vector<int> v;for (int i = 0; i < 10; i++) {v.push_back(i);}for_each(v.begin(), v.end(), print01);//普通函数cout << endl;for_each(v.begin(), v.end(), print02());//仿函数的函数对象cout << endl;return 0;
}

总结:for_each 在实际开发中是最常用的遍历算法,需要熟练掌握

1.2 transform

功能描述:

  • 搬运容器到另一个容器中

函数原型:

  • transform(iterator beg1, iterator end1, iterator beg2, _func);
    // beg1 源容器开始迭代器
    // end1 源容器结束迭代器
    // beg2 目标容器开始迭代器
    // _func 函数或者函数对象(可以在搬运过程中对源容器中的元素做一些逻辑运算等操作)

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;//仿函数
class Transform
{
public:int operator()(int val) {//搬运过程中需要把元素返回回去,所以返回类型为搬运的元素类型return val;}
};class MyPrint
{
public:void operator()(int val) {cout << val << " ";}
};int main() {vector<int> v;for (int i = 0; i < 10; i++) {v.push_back(i);}vector<int> vTarget;vTarget.resize(v.size());//目标容器需要提前开辟预定大小的空间transform(v.begin(), v.end(), vTarget.begin(), Transform());for_each(vTarget.begin(), vTarget.end(), MyPrint());cout << endl;return 0;
}

总结:

  • 搬运的目标容器必须要提前开辟预定大小的空间,否则无法正常搬运
  • 搬运过程中需要把元素返回回去,所以仿函数的返回类型为 搬运的元素的类型

2. 常用查找算法

学习目标:

  • 掌握常用的查找算法

算法简介:

  • find // 查找元素
  • find_if // 按条件查找元素
  • adjacent_find // 查找相邻重复元素
  • binary_search // 二分查找法
  • count // 统计元素个数
  • count_if // 按条件统计元素个数

2.1 find

功能描述:

  • 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器(end)

函数原型:

  • find(iterator beg, iterator end, value);
    // 以迭代器的方式按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
    // beg 开始迭代器
    // end 结束迭代器
    // value 查找的元素

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;class Person {
public:Person(string name, int age) {this->name = name;this->age = age;}bool operator==(const Person& p) {if (this->name == p.name && this->age == p.age) {return true;} else {return false;}}public:string name;int age;
};int main() {
//查找内置数据类型vector<int> v;for (int i = 0; i < 10; i++) {v.push_back(i);}vector<int>::iterator it = find(v.begin(), v.end(), 5);if (it == v.end()) {cout << "没有找到!" << endl;}else {cout << "找到:" << *it << endl;}//查找自定义数据类型vector<Person> vp;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);vp.push_back(p1);vp.push_back(p2);vp.push_back(p3);vp.push_back(p4);Person pp("bbb", 20);//当使用 find 查找自定义数据类型时,要在自定义数据类型内部重载 == 运算符,让底层 find 知道如何对比自定义数据类型vector<Person>::iterator it = find(vp.begin(), vp.end(), pp);if (it == vp.end()) {cout << "没有找到" << endl;}else {cout << "找到:" << it->name << " " << it.age << endl;}return 0;
}

总结:

  • 利用 find 可以在容器中找指定的元素,返回值是迭代器
  • 当使用 find 查找自定义数据类型时,要在自定义数据类型内部重载 == 运算符,让底层 find 知道如何对比自定义数据类型

2.2 find_if

功能描述:

  • 按条件查找元素

函数原型:

  • find_if(iterator beg, iterator end, _Pred);
    // 按值查找元素,找到返回指定元素迭代器位置,找不到返回结束迭代器位置
    // beg 开始迭代器
    // end 结束迭代器
    // _Pred 函数或者谓词(返回 bool 类型的仿函数)

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;//内置数据类型仿函数
class GreaterFive
{
public:bool operator()(int val) {return val > 5;}
};class Person {
public:Person(string name, int age) {this->name = name;this->age = age;}bool operator==(const Person& p) {if (this->name == p.name && this->age == p.age) {return true;} else {return false;}}public:string name;int age;
};//自定义数据类型仿函数
class Greater20
{
public:bool operator()(Person& p) {return p.age > 20;}
};int main() {
//查找内置数据类型vector<int> v;for (int i = 0; i < 10; i++) {v.push_back(i);}vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());if (it == v.end()) {cout << "没有找到!" << endl;}else {cout << "找到大于 5 的数字为:" << *it << endl;}//查找自定义数据类型vector<Person> vp;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);vp.push_back(p1);vp.push_back(p2);vp.push_back(p3);vp.push_back(p4);Person pp("bbb", 20);vector<Person>::iterator it = find(vp.begin(), vp.end(), Greater20());if (it == vp.end()) {cout << "没有找到" << endl;}else {cout << "找到:" << it->name << " " << it.age << endl;}return 0;
}

2.3 adjacent_find

功能描述:

  • 查找相邻重复元素

函数原型:

  • adjacent_find(iterator beg, iterator end);
    // 查找相邻重复元素,返回相邻元素的第一个位置的迭代器;找不到返回结束迭代器位置。
    // beg 开始迭代器
    // end 结束迭代器

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {
//查找内置数据类型vector<int> v;v.push_back(0);v.push_back(2);v.push_back(0);v.push_back(3);v.push_back(1);v.push_back(4);v.push_back(3);v.push_back(3);vector<int>::iterator it = adjacent_find(v.begin(), v.end());if (it == v.end()) {cout << "没有找到相邻重复元素!" << endl;}else {cout << "找到相邻重复元素:" << *it << endl;}return 0;
}

2.4 binary_search

功能描述:

  • 查找指定元素是否存在

函数原型:

  • bool binary_search(iterator beg, iterator end, val);
    // 查找指定的元素,查到返回 true,否则 false
    // beg 开始迭代器
    // end 结束迭代器
    // value 查找的元素

注意:容器必须是有序的序列,在无序序列中不可用。(如果是无序的序列,结果未知!)

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int main() {
//查找内置数据类型vector<int> v;for (int i = 0; i < 10; i++) {v.push_back(i);}bool ret= binary_search(v.begin(), v.end(), 9);if (ret) {cout << "找到了元素!" << endl;}else {cout << "未找到!" << endl;}return 0;
}

总结:虽然二分查找法查找效率很高,但是值得注意的是查找的容器中元素必须是有序序列。

2.5 count

功能描述:

  • 统计元素个数

函数原型:

  • count(iterator beg, iterator end, val);
    // 统计元素出现次数
    // beg 开始迭代器
    // end 结束迭代器
    // value 统计的元素

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;class Person {
public:Person(string name, int age) {this->name = name;this->age = age;}bool operator==(const Person& p) {//底层要求必须加上 const,否则会报错if (this->age == p.age) {return true;} else {return false;}}public:string name;int age;
};int main() {
//统计内置数据类型vector<int> v;v.push_back(0);v.push_back(2);v.push_back(0);v.push_back(3);v.push_back(1);v.push_back(4);v.push_back(3);v.push_back(3);int num = count(v.begin(), v.end(), 3);cout << "3 的元素个数为:" << num << endl;//统计自定义数据类型vector<Person> vp;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 20);Person p4("ddd", 40);vp.push_back(p1);vp.push_back(p2);vp.push_back(p3);vp.push_back(p4);Person pp("eee", 20);//当使用 count 查找自定义数据类型时,要在自定义数据类型内部重载 == 运算符,让底层 count 知道如何对比自定义数据类型num = count(vp.begin(), vp.end(), pp);cout << "和 eee 的年龄相同 的元素个数为:" << num << endl;return 0;
}

总结:统计自定义数据类型的时候,需要配合重载 operator==

2.5 count_if

功能描述:

  • 按条件统计元素个数

函数原型:

  • count_if(iterator beg, iterator end, _Pred);
    // 按条件统计元素出现次数
    // beg 开始迭代器
    // end 结束迭代器
    // _Pred 谓词

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;class Greater2
{
public:bool operator()(int val) {return val > 2;}
};class Person {
public:Person(string name, int age) {this->name = name;this->age = age;}bool operator==(const Person& p) {//底层要求必须加上 const,否则会报错if (this->age == p.age) {return true;} else {return false;}}public:string name;int age;
};class AgeGreater20
{
public:bool operator()(const Person& p) {return p.age >= 20;}
};int main() {
//统计内置数据类型vector<int> v;v.push_back(0);v.push_back(2);v.push_back(0);v.push_back(3);v.push_back(1);v.push_back(4);v.push_back(3);v.push_back(3);int num = count_if(v.begin(), v.end(), Greater2());cout << "大于 2 的元素个数为:" << num << endl;//统计自定义数据类型vector<Person> vp;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 20);Person p4("ddd", 40);vp.push_back(p1);vp.push_back(p2);vp.push_back(p3);vp.push_back(p4);num = count(vp.begin(), vp.end(), AgeGreater20());cout << "age >= 20 的元素个数为:" << num << endl;return 0;
}

3. 常用排序算法

学习目标:

  • 掌握常用的排序算法

算法简介:

  • sort //对容器中元素进行排序
  • random_shuffle //洗牌 指定范围内的元素随机调整次序
  • merge //容器元素合并,并存储在另一个容器中
  • reverse //反转指定范围内的元素

3.1 sort

功能描述:

  • 对容器内元素进行排序

函数原型:

  • sort(iterator beg, iterator end, _Pred);
    // 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
    // beg 开始迭代器
    // end 结束迭代器
    // _Pred 谓词,可以选择填或不填,不填的话默认是一个从小到大的排序;如果填的话会按照指定的排序规则进行排序
    // 返回值是 bool 类型

示例:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;class Greater2
{
public:bool operator()(int val) {return val > 2;}
};//普通函数遍历
void myPrint(int val) {cout << val << " ";
}int main() {vector<int> v;v.push_back(0);v.push_back(2);v.push_back(3);v.push_back(1);v.push_back(4);sort(v.begin(), v.end());//1.默认升序排列for_each(v.begin(), v.end(), myPrint);cout << endl;sort(v.begin(), v.end(), greater<int>());//2.使用内建函数对象降序排列for_each(v.begin(), v.end(), myPrint);cout << endl;return 0;
}

总结:sort 属于开发中最常用的算法之一,需熟练掌握

3.2 random_shuffle

功能描述:

  • 洗牌:对指定范围内的元素随机调整次序

函数原型:

  • random_shuffle(iterator beg, iterator end);
    // 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
    // beg 开始迭代器
    // end 结束迭代器

示例:

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;//普通函数遍历
void myPrint(int val) {cout << val << " ";
}int main() {vector<int> v;for (int i = 0 ; i < 10; i++) {v.push_back(i);}//1.利用洗牌算法打乱顺序,但每次打乱顺序是一样的//random_shuffle(v.begin(), v.end());//2.利用洗牌算法打乱顺序,加入随机种子真实打乱srand((unsigned int)time(NULL));random_shuffle(v.begin(), v.end());for_each(v.begin(), v.end(), myPrint);cout << endl;return 0;
}

总结:random_shuffle 洗牌算法比较实用,使用时记得加随机数种子

3.3 merge

功能描述:

  • 两个容器元素合并,并存储到另一个容器中

函数原型:

  • merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
    // beg1 容器1开始迭代器
    // end1 容器1结束迭代器
    // beg2 容器2开始迭代器
    // end2 容器2结束迭代器
    // dest 目标容器开始迭代器

注意:默认情况下两个容器必须都是升序序列,合并后依然是一个升序序列;如果都是降序序列的话,需要在参数列表中第 6 个形参的位置填写 谓词参数 指定规则。

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;//普通函数遍历
void myPrint(int val) {cout << val << " ";
}int main() {vector<int> v1;for (int i = 0 ; i < 10; i++) {v1.push_back(i);}vector<int> v2;for (int i = 0 ; i < 10; i++) {v2.push_back(i+3);}vector<int> vTarget;vTarget.resize(v1.size() + v2.size());//提前给目标容器分配空间merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());for_each(vTarget.begin(), vTarget.end(), myPrint);cout << endl;return 0;
}

总结:

  • 默认情况下 merge 合并的两个容器必须都是升序序列,合并后依然是一个升序序列。
  • 必须提前给目标容器分配空间

3.3 reverse

功能描述:

  • 将容器内元素进行反转

函数原型:

  • reverse(iterator beg, iterator end);
    // 反转指定范围内的元素
    // beg 开始迭代器
    // end 结束迭代器

示例:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;//普通函数遍历
void myPrint(int val) {cout << val << " ";
}int main() {vector<int> v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);cout << "反转前" << endl;for_each(v.begin(), v.end(), myPrint);cout << endl;cout << "反转后" << endl;reverse(v.begin(), v.end());//1.for_each(v.begin(), v.end(), myPrint);cout << endl;return 0;
}

总结:

  • reverse 反转区间内元素,面试题可能涉及到

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

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

相关文章

机器人制作开源方案 | 网球自动拾取机

作者&#xff1a;柳文浩、李浩杰、苏伟男、贾思萌、张天芸 单位&#xff1a;西安外事学院 指导老师&#xff1a;胡宝权、陈小虎 1. 产品说明 1.1 设计目的 近年来&#xff0c;网球运动越来越受到老百姓的欢迎&#xff0c;各种规模的比赛层出不穷。然而由于网球运动极为激烈…

中国改性聚丙烯产业调研与投资战略报告(2023版)

内容介绍&#xff1a; 改性聚丙烯就是基于聚丙烯原料对其性能和其他方面的一些改进&#xff0c;如增强聚丙烯材料的冲击、拉伸强度、弹性等。聚丙烯改性主要通过化学改性和物理改性进行调整&#xff0c;使其在拥有本身优异性能的同时可以兼顾其他有利于生产和使用的性能。改性…

Python3基础

导包 在 python 用 import 或者 from...import 来导入相应的模块。 将整个模块(somemodule)导入&#xff0c;格式为&#xff1a; import somemodule 从某个模块中导入某个函数,格式为&#xff1a; from somemodule import somefunction 从某个模块中导入多个函数,格式为&#…

C++基础 -4- C/C++混合编程

引用格式(图片代码段呈现) extern "C" {#include "string.h" }代码验证 &#xff08;分别使用了C/C 的标准输出&#xff09; #include "iostream"using namespace std;extern "C" { #include "stdio.h" #include "…

Win11修改用户名(超详细图文)

新买的电脑一般预装Windows11系统&#xff08;家庭与学生版&#xff09;&#xff0c;新电脑初次开机使用微软邮箱账号登录&#xff0c;则系统将用户名自动设置成邮箱前5位字符。我的用户名便是一串数字【231xx】&#xff08;qq邮箱前5位&#xff09;&#xff0c;看着很不舒服&a…

快速掌握Pyqt5的10种容器(Containers)

快速掌握Pyqt5的三种主窗口 快速掌握Pyqt5的2种弹簧 快速掌握Pyqt5的5种布局 快速弄懂Pyqt5的5种项目视图&#xff08;Item View&#xff09; 快速弄懂Pyqt5的4种项目部件&#xff08;Item Widget&#xff09; 快速掌握Pyqt5的6种按钮 快速掌握Pyqt5的20种输入控件&#xff08;…

属性级情感分析

笔记为自我总结整理的学习笔记&#xff0c;若有错误欢迎指出哟~ 属性级情感分析 简介数据集介绍数据加载和预处理&#xff08;data_utils.py&#xff09;预训练模型&#xff08;skep&#xff09;模型定义模块&#xff08;model.py&#xff09;训练配置&#xff08;config.py&am…

element 的 Notification 通知,自定义内容

通知事件&#xff1a; // 商户后台通知 MerchantBackgroundNotice() {// 禁止消息通知弹出多条if(this.notifyInstance) {this.notifyInstance.close();}const h this.$createElement; // 创建文本节点this.notifyInstance this.$notify({showClose: false, // 禁止关闭按钮…

Powerbuilder中利用API实现动画特效

Powerbuilder中利用API实现动画特效 摘要:本文通过在Powerbuilder中调用Windows API函数,实现任意图片的放大及任意图片从正面逐渐翻转到背面的功能 关键词:内存设备描述表 显示器设备描述表 API 一、问题的提出 Powerbuilder是一个面向对象的开发大型数据库的图形化的前端开…

vue随意置换页面元素位置

写在前面&#xff0c;博主是个在北京打拼的码农&#xff0c;从事前端工作5年了&#xff0c;做过十多个大大小小不同类型的项目&#xff0c;最近心血来潮在这儿写点东西&#xff0c;欢迎大家多多指教。 对于文章中出现的任何错误请大家批评指出&#xff0c;一定及时修改。有任何…

数据结构 / 顺序表操作 / 顺序表尾部删除

1.实现逻辑 只需要把顺序表的长度减1。 2.顺序表尾部删除函数代码 /**顺序表尾部删除* */int delete_tail(sqlist *list) {if(NULLlist || 1is_list_empty(list)){return -1;}list->len--;return 0;}3. 完整代码 #include <string.h> #include <stdlib.h> #in…

Redis Cluster主从模式详解

在软件的架构中&#xff0c;主从模式&#xff08;Master-Slave&#xff09;是使用较多的一种架构。主&#xff08;Master&#xff09;和从&#xff08;Slave&#xff09;分别部署在不同的服务器上&#xff0c;当主节点服务器写入数据时&#xff0c;同时也会将数据同步至从节点服…

图解算法数据结构-LeetBook-树03_层序遍历奇数偶数行方向不同

一棵圣诞树记作根节点为 root 的二叉树&#xff0c;节点值为该位置装饰彩灯的颜色编号。请按照如下规则记录彩灯装饰结果&#xff1a; 第一层按照从左到右的顺序记录 除第一层外每一层的记录顺序均与上一层相反。即第一层为从左到右&#xff0c;第二层为从右到左。 示例 1&…

自动化测试-Selenium

一. Selenium介绍 selenium 是用来做web自动化测试的框架,支持各种浏览器,各种,支持各种语言 原理: 二. 元素定位 2.1 XPath 定位 绝对路径: /html/head/title 相对路径以双斜杠开头,常见的相对路径定位有以下几种: <1>相对路径索引: 索引是从1开始的 <2>相…

语音识别学习笔记

目录 开源的语音识别项目 端到端的多说话人语音识别序列化训练方法简介 新一代 Kaldi: Two-pass 实时语音识别 开源的语音识别项目 有哪些语音识别的开源项目&#xff1f; - 知乎 端到端的多说话人语音识别序列化训练方法简介 端到端的多说话人语音识别序列化训练方法简介 …

探索深度学习:从理论到实践的全面指南

探索深度学习&#xff1a;从理论到实践的全面指南 摘要&#xff1a; 本文旨在提供一个关于深度学习的全面指南&#xff0c;带领读者从理论基础到实践应用全方位了解这一技术。我们将介绍深度学习的历史、基本原理、常用算法和应用场景&#xff0c;并通过Python代码示例和Tens…

STM32使用多路PWM注意事项

这是使用CubeMX自动产生的代码&#xff0c;使用TIM2产生了PA0,PA1,PA2,PA3这4路PWM&#xff0c;可以看到里面Pulse是共同使用了一个sConfigOC,如果是需要动态调整Pulse&#xff0c;就需要特别注意。 如果是用来产生呼吸灯&#xff0c;就会把这4个PWM都打乱&#xff0c;我觉得&a…

Go查询Elasticsearch

在 Go 中需要在 Elasticsearch 中执行带有过滤条件的查询时&#xff0c;你可以使用 github.com/olivere/elastic 库的过滤器&#xff08;Filter&#xff09;功能。以下是一个示例代码&#xff0c;展示了如何在 Go 中使用 Elasticsearch 进行带有过滤条件的分页查询&#xff1a;…

【nlp】4.4 Transformer库的使用(管道模式pipline、自动模式auto,具体模型BertModel)

Transformer库的使用 1 了解Transformers库2 Transformers库三层应用结构3 管道方式完成多种NLP任务3.1 文本分类任务3.2 特征提取任务3.3 完型填空任务3.4 阅读理解任务3.5 文本摘要任务3.6 NER任务4 自动模型方式完成多种NLP任务4.1 文本分类任务4.2 特征提取任务4.3 完型填空…

讯飞星火知识库文档问答Web API的使用(二)

上一篇提到过星火spark大模型&#xff0c;现在有更新到3.0&#xff1a; 给ChuanhuChatGPT 配上讯飞星火spark大模型V2.0&#xff08;一&#xff09; 同时又看到有知识库问答的web api&#xff0c;于是就测试了一下。 下一篇是在ChuanhuChatGPT 中单独写一个基于星火知识库的内容…