C++STL总结笔记(三)—— 常见算法

文章目录

  • 一、基本概念
  • 二、程序示例
    • 1.遍历
    • 2. 查找
    • 3. 排序、拷贝、替换
    • 4. numeric相关算法
  • 总结


一、基本概念

算法是STL中很重要的一部分,其功能包括比较,查找,排序,交换,遍历,复制等等。

最大的算法头文件是algorithm,封装了很多种模板类。还有numeric和functional也比较常见。

二、程序示例

1.遍历

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;void print(int a)
{cout << a << " ";
}class print1
{
public:void operator()(int a){cout << a << " ";}
};class print2
{
public:int operator()(int a){cout << a << " ";return a;}
};void test()
{list<int>L;L.push_back(1);L.push_back(2);L.push_back(3);//函数作为形参进行遍历输出for_each(L.begin(), L.end(), print);cout << endl;//仿函数进行遍历for_each(L.begin(), L.end(), print1());cout << endl;//transform实现遍历list<int>L1;L1.resize(L.size());transform(L.begin(),L.end(),L1.begin(), print2());}int main()
{test();system("pause");
}

2. 查找

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;class Compare
{
public://一元谓词bool operator()(int a){return a > 1;}
};class Cat
{
public:Cat(string name, int color, int age){this->Name = name;this->Color = color;this->Age = age;}//自定义数据类型需要重载==bool operator==(const Cat& cat){if (Name == cat.Name && Color == cat.Color && Age == cat.Age){return true;}else{return false;}}public:string Name;int Color;int Age;
};class print1
{
public:bool operator()(Cat& cat){return cat.Age > 3;}
};void test()
{list<int>L;L.push_back(1);L.push_back(2);L.push_back(3);//find算法查找list<int>::iterator i = find(L.begin(), L.end(), 1);if (i == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << *i<<endl;}//find_if查找list<int>::iterator i1 = find_if(L.begin(),L.end(),Compare());if (i1 == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << *i1 << endl;}//查找元素是否存在,无序序列结果未知bool i2 = binary_search(L.begin(), L.end(), 1);if (i2){cout << "查找到1" << endl;}else{cout << "未查找到" << endl;}//count统计int n = count(L.begin(), L.end(),1);cout << n << endl;//count_if统计int n1 = count_if(L.begin(), L.end(), Compare());cout << n1 << endl;}void test1()
{list<Cat>L;Cat cat1("小100", 76, 2);Cat cat2("小200", 32, 2);Cat cat3("小300", 32, 4);Cat cat4("小400", 32, 3);Cat cat5("小500", 54, 1);//插入L.push_back(cat1);L.push_back(cat2);L.push_back(cat3);L.push_back(cat4);L.push_back(cat5);//find查找list<Cat>::iterator i = find(L.begin(), L.end(), cat1);if (i == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << (*i).Name << endl;}//find_if查找list<Cat>::iterator i1 = find_if(L.begin(), L.end(), print1());if (i1 == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << (*i1).Name << endl;}//查找相邻的重复元素list<Cat>::iterator i2 = adjacent_find(L.begin(), L.end());if (i2 == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << (*i2).Name << endl;}Cat cat6("小300", 32, 4);//count统计,需要重载int n = count(L.begin(), L.end(), cat6);cout << n << endl;
}int main()
{test1();system("pause");
}

3. 排序、拷贝、替换

#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;class Compare
{
public://一元谓词bool operator()(int a,int b){return a > b;}
};class Compare3
{
public://一元谓词bool operator()(int a){return a > 3;}
};void print(int a)
{cout << a << " ";
}class Cat
{
public:Cat(string name, int color, int age){this->Name = name;this->Color = color;this->Age = age;}//自定义数据类型需要重载==bool operator==(const Cat& cat){if (Name == cat.Name && Color == cat.Color && Age == cat.Age){return true;}else{return false;}}public:string Name;int Color;int Age;
};void test()
{vector<int>L;L.push_back(1);L.push_back(2);L.push_back(3);L.push_back(4);//降序sort(L.begin(), L.end(),Compare());for_each(L.begin(), L.end(), print);cout << endl;//greater<int>()sort(L.begin(), L.end(),greater<int>());for_each(L.begin(), L.end(), print);cout << endl;sort(L.begin(), L.end());//随机打乱//srand((unsigned int)time(NULL));//random_shuffle(L.begin(), L.end());//for_each(L.begin(), L.end(), print);//cout << endl;vector<int>L1(L);vector<int>L2;L2.resize(L.size()+L1.size());//合并,默认只能同为升序的合并merge必须为有序序列merge(L.begin(), L.end(), L1.begin(), L1.end(),L2.begin());for_each(L2.begin(), L2.end(), print);cout << endl;//反转reverse(L.begin(), L.end());for_each(L.begin(), L.end(), print);cout << endl;//拷贝vector<int>L3;L3.resize(L.size());copy(L.begin(), L.end(), L3.begin());for_each(L3.begin(), L3.end(), print);cout << endl;//替换replace(L.begin(), L.end(), 2, 5);for_each(L.begin(), L.end(), print);cout << endl;replace_if(L.begin(), L.end(), Compare3(),20);for_each(L.begin(), L.end(), print);cout << endl;//互换swap(L, L1);
}int main()
{test();system("pause");
}

4. numeric相关算法

#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
#include<ctime>
using namespace std;void print(int a)
{cout << a << " ";
}void test()
{vector<int>L;L.push_back(1);L.push_back(2);L.push_back(3);L.push_back(4);//计算容器元素的和,0为起始累加值int total = accumulate(L.begin(), L.end(), 0);cout << total<<endl;//填充元素vector<int>L1;L1.resize(L.size());fill(L1.begin(), L1.end(), 3);for_each(L1.begin(), L1.end(), print);cout << endl;//求交集vector<int>L2;L2.resize(min(L.size(),L1.size()));vector<int>::iterator i = set_intersection(L.begin(), L.end(), L1.begin(), L1.end(), L2.begin());for_each(L2.begin(), i, print);cout << endl;//求并集vector<int>L3;L3.resize(L.size()+ L1.size());vector<int>::iterator j = set_union(L.begin(), L.end(), L1.begin(), L1.end(), L3.begin());for_each(L3.begin(), j, print);cout << endl;//差集vector<int>L4;L4.resize(max(L.size() , L1.size()));vector<int>::iterator j1 = set_difference(L.begin(), L.end(), L1.begin(), L1.end(), L4.begin());for_each(L4.begin(), j1, print);
}int main()
{test();system("pause");
}

总结

以上只是stl算法中常见的,后续会随时补充新的算法。

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

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

相关文章

Java zip解压,并遍历zip中的配置文件 .cfg或.properties

1.解析cfg或properties配置文件 讲配置文件&#xff0c;读取&#xff0c;并封装成为map类型数据 /*** 解析cfg文件** param cfgFile* return*/public static Map<String, Object> readCfg(FileInputStream cfgFile) {Properties prop new Properties();Map<String, O…

db2 常用配置

db2set配置&#xff1a; db2set DB2_ENABLE_LDAPNO db2set DB2_ALTERNATE_GROUP_LOOKUPGETGROUPLIST db2set DB2_RESTORE_GRANT_ADMIN_AUTHORITIESON db2set DB2_SKIPINSERTEDON db2set DB2_LOAD_COPY_NO_OVERRIDENONRECOVERABLE db2set DB2_EVALUNCOMMITTEDON db2set DB2_SKIP…

安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(三)码农网

12. 安装 Apache Tomcat Tomcat 是由 Apache 设计的用来运行 Java HTTP web 服务器的 servlet 容器。按照下面的方法安装 tomcat&#xff0c;但需要指出的是安装 tomcat 之前必须先安装 Java。 # yum install tomcat 安装 Apache Tomcat 安装完 tomcat 之后&#xff0c;启动 to…

【图像处理】——图像特效处理(马赛克、图像融合、毛玻璃等)

参考:https://blog.csdn.net/qq_43328040/article/details/109081414 import cv2 import numpy as np import random#马赛克:将一定大小窗口的RGB设置成一个颜色 def horseBox(img):row,col,chal = img.shapeboxRow = int(0.3*row)boxcol = int(0.3*col)for m in range(50,b…

JDK5.0新特性之:泛型

文/陈刚 2005-11-09 一、前言 泛型这个词在现在的JAVA挺时髦&#xff0c;光从字面上你是无法知道它代表些什么东东的&#xff0c;所以我们还是不要从字面去理解&#xff0c;而是从一些实例去了解它吧。 二、泛型之前的日子 &#xff2a;&#xff24;&#xff2b;&#xff11;.…

QT5.14.2基于PCL1.11.1显示点云(基于Windows VS2019开发环境)

文章目录一、安装1.1 PCL安装1.2 QT安装1.3 VTK编译二、程序配置1. 基于mscv创建QT的程序2. 配置QT工程文件和依赖项3. 编写点云显示的小程序总结一、安装 1.1 PCL安装 PCL1.11.1库的安装网上教程很多&#xff0c;推荐一个很好的教程&#xff1a; Win10 系统下 VisualStudio2…

Spring学习笔记—最小化Spring XML配置

自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素&#xff0c;让Spring自动识别如何装配Bean的依赖关系。 自动检测(autodiscovery)比自动装配更进了一步&#xff0c;让Spring能够自动识别哪些类需要被配置成Spring Bean&#xf…

【数据结构】——快速排序

目录 一、代码 二、复杂度&#xff1a;O(nlog(n)) 三、快速排序的劣势 视频参考链接&#xff1a;https://www.bilibili.com/video/BV1mp4y1D7UP?p17 一、代码 思想&#xff1a;假设是对一个list进行排序 1、选取第一个元素作为p元素&#xff1b; 2、将p元素归位&#xff0…

读取数据库信息构建视图字段的备注信息,方便程序代码生成

在很多情况下&#xff0c;我们开发都需要有一个快速的代码生成工具用来提高开发效率&#xff0c;代码生成工具很多信息都是读取数据库的表、视图等元数据进行对象表信息的完善&#xff0c;有了这些信息&#xff0c;我们就可以在普通的实体类代码里面添加属性字段的中文注释&…

Ubuntu DNS bind9 配置

下面的配置就是实现解析test.zp.com到不同的IP地址 安装dns server软件包$ apt-get install bind9 配置dns配置文件的路径在/etc/bind路径下面添加一个zone$ /etc/bind# vim /etc/bind/named.conf.local 添加下面&#xff0c;语法可以参照/etc/bind/zones.rfc1918中的语法添加&…

微博分享错误

昨天再做这块的时候&#xff0c;不知怎么的点击之后什么反应都没有&#xff0c;程序也没有崩&#xff0c;日志倒是输出了这个错误 解决办法&#xff1a;打开你写分享的代码跟API文档对比一下创建文本、图片或者网页的时候是不是少写了那个属性&#xff0c;我这里是在创建网页的…

C++总结笔记(十二)—— 智能指针

文章目录前言一、智能指针是什么&#xff1f;二、示例总结前言 C对于内存管理的要求很高&#xff0c;如果不及时释放对象内存&#xff0c;就可能会发生内存泄露或野指针等情况&#xff0c;鉴于这种情况&#xff0c;C11提出了智能指针的概念。 一、智能指针是什么&#xff1f;…

代码生成工具之界面快速生成

界面开发&#xff0c;无论对于Web开发&#xff0c;还是Winform开发&#xff0c;都需要耗费一定的时间&#xff0c;特别对于一个数据库字段比较多的界面&#xff0c;一般就需要在编辑界面上摆的更多的控件来做数据显示&#xff0c;每次碰到这个&#xff0c;都有点头痛&#xff0…

javascript - 封装原生js实现ajax

1 /*2 * ajax方法3 */4 var Ajax function() {5 var that this;6 //创建异步请求对象方法7 that.createXHR function() {8 if(window.XMLHttpRequ…

QT对象树、信号和槽机制

文章目录一 、对象树是什么&#xff1f;二、信号和槽的基本概念2.1 信号2.2 槽2.3 松散耦合2.4 特点三、示例总结一 、对象树是什么&#xff1f; 对象树是由父类和若干子类对象组成&#xff0c;而子类也可以由若干孙类。 QT中的对象树是以QObject为起始父类来完成树的构建的&a…

【数据结构】——归并排序

目录 一、代码 二、随笔 一、代码 归并排序的主要思路&#xff1a;将两个有序的子列表归并为一个有序的大列表 #归并函数&#xff0c;假设li是由左右两个有序的子列表组成,假设两个子列表都是从小到大排好序的列表 def merge(li,low,mid,high)::param li: 由左右两个有序的子列…

开发发布npm module包

开发发布npm module包 问题 在项目开发过程中&#xff0c;每当进入一个新的业务项目&#xff0c;从零开始搭建一套前端项目结构是一件让人头疼的事情&#xff0c;就要重新复制一个上一个项目的前端框架和组件代码库。其中很多功能的模块组件都要重复拷贝&#xff0c;可以统一将…

如何使用ATS提高应用的安全性

App Transport Security&#xff0c;简短的说就是ATS&#xff0c;是iOS9和OS X El Capitan的一个新特性。App Transport Security 的目标是提高Apple 操作系统的安全性以及在此操作系统上运行的任何应用的安全性。 基于HTTP传输数据的网络请求都是明文。开启App Transport Secu…

手机客户端测试考虑的点

手机客户端测试考虑点总结 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 此文未本人工作中的总结&#xff0c;特此总结。 异常场景&#xff1a; 网络异常&#xff0c;服务器异常&#xff0c;接口异常或参考参数篡改&#xff0c;断电&#xff0c;…

NMS(非极大值抑制)算法详解与示例

一、NMS是什么&#xff1f; NMS&#xff08;non maximum suppression&#xff09;即非极大值抑制&#xff0c;广泛应用于传统的特征提取和深度学习的目标检测算法中。 NMS原理是通过筛选出局部极大值得到最优解。 在2维边缘提取中体现在提取边缘轮廓后将一些梯度方向变化率较小…