C++之常用算法

C++之常用算法

在这里插入图片描述

for_each

transform

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>class Tranfor 
{
public:int operator()(int var){return var;}
};class MyPrint
{
public:void operator()(int var){cout << var<<" " ;}
};
void test()
{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(),Tranfor());//遍历目标容器for_each(vTarget.begin(), vTarget.end(),MyPrint());cout << endl;
}int main()
{test();system("pause");return 0;
}

在这里插入图片描述

常用查找算法

find

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//查找内置数据类型void test()
{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;}
}//查找自定义数据类型
class Person
{
public:Person(string name,int age){this->m_Name = name;this->m_Age = age;}//重载== 底层find知道该如何对比Person数据类型bool operator==(const Person&p){if (this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};void test2()
{//定义容器vector<Person>v1;//创建数据(对象)Person p1("aaa", 20);Person p2("bbb", 30);Person p3("ccc", 40);Person p4("ddd", 50);//将数据放入容器v1.push_back(p1);v1.push_back(p2);v1.push_back(p3);v1.push_back(p4);//查到对象数据vector<Person>::iterator it = find(v1.begin(), v1.end(), p2);if (it == v1.end()){cout << "没有找到" << endl;}else{cout << "找到了  名字为:" << it->m_Name << "年龄为:" << it->m_Age << endl;}
}int main()
{test2();system("pause");return 0;
}

在这里插入图片描述

find_if

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//查找内置数据类型
class Greater
{
public:bool operator()(int val){return val > 5;}
};void test()
{vector<int>v;for (int i = 0;i < 10;i++){v.push_back(i);}vector<int>::iterator it = find_if(v.begin(),v.end(), Greater());if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到了" <<  *it << endl;}
}//查找自定义数据类型
class Person
{
public:Person(string name,int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};
class GreatAge
{
public:bool operator()(Person&p){return p.m_Age > 20;}
};
void test2()
{//定义容器vector<Person>v1;//创建数据(对象)Person p1("aaa", 20);Person p2("bbb", 30);Person p3("ccc", 40);Person p4("ddd", 50);//将数据放入容器v1.push_back(p1);v1.push_back(p2);v1.push_back(p3);v1.push_back(p4);//查到对象数据vector<Person>::iterator it = find_if(v1.begin(), v1.end(), GreatAge());if (it == v1.end()){cout << "没有找到" << endl;}else{cout << "找到了  名字为:" << it->m_Name << "年龄为:" << it->m_Age << endl;}
}int main()
{test2();system("pause");return 0;
}

在这里插入图片描述

adjacent_find

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>void test()
{vector<int>v;v.push_back(9);v.push_back(3);v.push_back(4);v.push_back(5);v.push_back(5);vector<int>::iterator it = adjacent_find(v.begin(),v.end());if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到了" <<  *it << endl;}
}int main()
{test();system("pause");return 0;
}

在这里插入图片描述

binary_search

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>void test()
{vector<int>v;for (int i = 0;i < 10;i++){v.push_back(i);}//注意:容器必须是有序的序列bool rat =  binary_search(v.begin(), v.end(), 9);if (rat == true){cout << "找到了" << endl;}else{cout << "没有找到" << endl;}
}int main()
{test();system("pause");return 0;
}

在这里插入图片描述

count

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>void test()
{vector<int>v;v.push_back(30);v.push_back(50);v.push_back(30);v.push_back(20);v.push_back(50);int num = count(v.begin(), v.end(), 50);cout << "找到元素50" << num << endl;
}//查找自定义数据类型
class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}//重载== 底层find知道该如何对比Person数据类型bool operator==(const Person& p){if (this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};void test2()
{//定义容器vector<Person>v1;//创建数据(对象)Person p1("aaa", 20);Person p2("bbb", 30);Person p3("ccc", 40);Person p4("ddd", 50);//将数据放入容器v1.push_back(p1);v1.push_back(p2);v1.push_back(p3);v1.push_back(p4);//查到对象数据Person p5("ddd", 50);int num = count(v1.begin(), v1.end(), p5);cout << "和ddd同岁的人" << num << endl;
}
int main()
{test2();system("pause");return 0;
}

在这里插入图片描述
在这里插入图片描述

count_if

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
class Greater
{
public:bool operator()(int val){return val > 40;}
};
void test()
{vector<int>v;v.push_back(30);v.push_back(50);v.push_back(30);v.push_back(20);v.push_back(50);int num = count_if(v.begin(), v.end(), Greater());cout << "找到大于40的个数" << num << endl;
}//查找自定义数据类型
class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};
class GreatAge
{
public:bool operator()(Person& p){return p.m_Age > 20;}
};
void test2()
{//定义容器vector<Person>v1;//创建数据(对象)Person p1("aaa", 20);Person p2("bbb", 30);Person p3("ccc", 40);Person p4("ddd", 50);//将数据放入容器v1.push_back(p1);v1.push_back(p2);v1.push_back(p3);v1.push_back(p4);int num = count_if(v1.begin(), v1.end(), GreatAge());cout << "岁数大于20的个数" << num << endl;
}
int main()
{test2();system("pause");return 0;
}

在这里插入图片描述

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

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

相关文章

23.11.19日总结

经过昨天的中期答辩&#xff0c;其实可以看出来项目进度太慢了&#xff0c;现在是第十周&#xff0c;预计第十四周是终级答辩&#xff0c;在这段时间要把项目写完。 前端要加上一个未登录的拦截器&#xff0c;后端加上全局的异常处理。对于饿了么项目的商品建表&#xff0c;之前…

面向对象与面向过程的区别

面向对象 以对象为中心&#xff0c;把数据封装成为一个整体&#xff0c;其他数据无法直接修改它的数据&#xff0c;将问题分解成不同对象&#xff0c;然后给予对象相应的属性和行为。 面向过程 关注代码过程&#xff0c;直接一程序来处理数据&#xff0c;各模块之间有调用与…

mybatis使用xml形式配置

以这个注解形式的查询代码为例 Select("select * from emp where name like concat(%,#{name},%) and gender #{gender} and entrydate between #{begin} and #{end} order by update_time desc ")public List<Emp> list(String name, Short gender, LocalDat…

Unity中Shader法线贴图(上)

文章目录 前言一、法线纹理的作用二、为什么法线贴图长这样&#xff1f;&#xff08;蓝色&#xff09;三、法线贴图能使纹理采样时&#xff0c;进行偏移采样四、在Shader中使用法线贴图1、在属性面板定义一个变量来接收法线贴图2、在使用前声明 _NormalTex3、在片元着色器中&am…

【ATTCK】MITRE Caldera-路径发现插件

CALDERA是一个由python语言编写的红蓝对抗工具&#xff08;攻击模拟工具&#xff09;。它是MITRE公司发起的一个研究项目&#xff0c;该工具的攻击流程是建立在ATT&CK攻击行为模型和知识库之上的&#xff0c;能够较真实地APT攻击行为模式。 通过CALDERA工具&#xff0c;安全…

SQLite 安装和 Java 使用教程

SQLite是一个C语言库&#xff0c;它实现了一个小型、快速、自包含、高可靠性、功能齐全的SQL数据库引擎。SQLite是世界上使用最多的数据库引擎。SQLite内置于所有手机和大多数计算机中&#xff0c;并捆绑在人们每天使用的无数其他应用程序中。 SQLite文件格式稳定、跨平台、向…

数据结构【DS】特殊二叉树

完全二叉树 叶子结点只能出现在最下层和次下层, 最下层的叶子结点集中在树的左部完全二叉树中, 度为1的节点数 0个或者1个【计算时可以用这个快速计算, 配合&#x1d45b;0&#x1d45b;21】若n为奇数&#xff0c;则分支节点每个都有左右孩子&#xff1b;若n为偶数&#xff0…

腾讯云轻量数据库是什么?性能如何?费用价格说明

腾讯云轻量数据库测评&#xff0c;轻量数据库100%兼容MySQL 5.7和8.0&#xff0c;腾讯云提供1C1G20GB、1C1G40GB、1C2G80GB、2C4G120GB、2C8G240GB五种规格轻量数据库&#xff0c;腾讯云百科txybk.com分享腾讯云轻量数据库测评、轻量数据库详细介绍、特性、配置价格和常见问题解…

Web3 分布式存储 IPFS(Web3项目一实战之四)

IPFS是一种分布式文件存储协议,它允许世界各地的计算机存储和服务文件作为一个巨大的对等网络的一部分来存储和服务文件。 世界上任何地方的任何计算机都可以下载IPFS软件并开始托管和提供文件。 如果有人在自己的计算机上运行IPFS,并将文件上传到IPFS网络,那么世界上其他任…

算法通关村第十关-青铜挑战快速排序

大家好我是苏麟,今天带来快速排序 . 快速排序 单边快速排序(lomuto 洛穆托分区方案) 单边循环 (lomuto分区) 要点 : 选择最右侧元素作为基准点j 找比基准点小的&#xff0c;i 找比基准点大的&#xff0c;一旦找到&#xff0c;二者进行交换。 交换时机: 找到小的&#xff0c…

【0到1学习Unity脚本编程】第一人称视角的角色控制器

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;【0…

linux文件IO

文件IO截断 截断对文件的偏移量没有影响。