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

【C++】STL-常用算法-常用查找算法

0.前言

在这里插入图片描述

1.find

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

#include <iostream>
using namespace std;// 常用查找算法 find
#include<vector>
#include<algorithm>//查找 内置数据类型
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找 容器中 是否有 5 这个元素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) //加const 防止修改数据{if (p.m_Name == this->m_Name && p.m_Age == this->m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};void test02()
{//创建数据Person p1("a", 10);Person p2("b", 20);Person p3("c", 30);//放入容器中vector<Person>v;v.push_back(p1);v.push_back(p2);v.push_back(p3);Person p5("b", 20);vector<Person>::iterator it = find(v.begin(), v.end(), p5);if (it == v.end()){cout << "未找到" << endl;}else{cout << "找到: 姓名: " << it->m_Name << "  age:" << (*it).m_Age << endl;}
}int main()
{test01();cout << "------------------------" << endl;test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
} 

在这里插入图片描述

2.find_if

在这里插入图片描述

#include <iostream>
using namespace std;// 常用查找算法 find_if
#include<vector>//1.查找内置数据类型class Greater5
{
public:bool operator()(int val){return val > 5;}
};void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>::iterator it = find_if(v.begin(), v.end(), Greater5());if (it == v.end()){cout << "no find" << endl;}else{cout << "find element: " << (*it) << endl;}
}//2、查找自定义数据类型class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};//方式一 利用仿函数
//class greater_Age20
//{
//public:
//	bool operator()(const Person& p)
//	{
//		return p.m_Age > 20;
//	}
//};//方式二 利用普通函数
bool greater_Age20(const Person& p)
{return p.m_Age > 20;
}void test02()
{//创建对象Person p1("a", 10);Person p2("b", 20);Person p3("c", 30);vector<Person>v;v.push_back(p1);v.push_back(p2);v.push_back(p3);//查找年龄大于20的人vector<Person>::iterator it = find_if(v.begin(), v.end(), greater_Age20);if (it == v.end()){cout << "no find" << endl;}else{cout << "find the element: name:" << it->m_Name << " age:" << it->m_Age << endl;}
}int main()
{test01();cout << "------------------------" << endl;test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
} 

在这里插入图片描述

3.adjacent_find

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

#include <iostream>
using namespace std;// 常用查找算法 adjacent_find
#include<vector>
#include<algorithm>void test01()
{//创建对象vector<int>v;v.push_back(1);v.push_back(2);v.push_back(1);v.push_back(3);v.push_back(4);v.push_back(3);v.push_back(3);vector<int>::iterator pos = adjacent_find(v.begin(), v.end());if (pos == v.end()){cout << "no find adjacent duplcate elements " << endl;}else{cout << "find adjacent duplcate elements: " << *pos << endl;}
}int main()
{test01();cout << "------------------------" << endl;//test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
} 

在这里插入图片描述

4.binary_search

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

#include <iostream>
using namespace std;// 常用查找算法 binary_search 二分查找
#include<vector>
#include<algorithm>void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//v.push_back(2);  如果是无序序列,结果未知!//查找容器中是否有9元素//注意:容器必须是有序的序列(从小到大)bool ret = binary_search(v.begin(), v.end(),9);if (ret){cout << "find point element" << endl;}else{cout << "no find" << endl;}
}int main()
{test01();cout << "------------------------" << endl;//test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
} 

在这里插入图片描述

5.count

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

#include <iostream>
using namespace std;// 常用查找算法 count
#include<vector>
#include<algorithm>//1.统计内置数据类型
void test01()
{vector<int>v;v.push_back(1);v.push_back(4);v.push_back(1);v.push_back(3);int num = count(v.begin(), v.end(), 1);cout << "the nmber of point element: " << num << endl;
}//2.统计自定义数据类型
class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}bool operator==(const Person& p){if (p.m_Age == this->m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};
void test02()
{//创建对象Person p1("刘备", 35);Person p2("薇恩", 24);Person p3("皮城", 35);Person p4("光辉", 40);vector<Person>v;v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);Person p5("卡米尔", 35);int num = count(v.begin(), v.end(), p5);cout << "跟卡米尔同岁的人有多少个: " << num << endl;
}int main()
{test01();cout << "------------------------" << endl;//test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
} 

在这里插入图片描述

6.count_if

在这里插入图片描述

#include <iostream>
using namespace std;// 常用查找算法 count_if
#include<vector>
#include<algorithm>//1、统计内置数据类型//利用仿函数
class Greater3
{
public:bool operator()(int val){return val > 3;}
};void test01()
{vector<int>v;v.push_back(1);v.push_back(6);v.push_back(3);v.push_back(2);v.push_back(4);v.push_back(3);int num = count_if(v.begin(), v.end(), Greater3());cout << "the number of element greater than 3 :  " << num << endl;
}//2、统计自定义数据类型class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};//利用普通函数
bool age_Greater35(const Person& p)
{if (p.m_Age > 35){return true;}else{return false;}
}void test02()
{//创建对象Person p1("刘备", 35);Person p2("薇恩", 24);Person p3("皮城", 35);Person p4("光辉", 40);vector<Person>v;v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//统计岁数大于35的人物的个数int num = count_if(v.begin(), v.end(), age_Greater35);cout << "the number of character whose age greater than 35 :" << num << endl;
}int main()
{test01();cout << "------------------------" << endl;test02();//cout << "------------------------" << endl << endl;//test03();//**************************************system("pause");return 0;
} 

在这里插入图片描述

http://www.lryc.cn/news/157945.html

相关文章:

  • vue3 webpack打包流程及安装 (1)
  • 【C++】内联函数 ① ( 内联函数引入 | 内联函数语法 )
  • 聊聊springboot的ConfigurationProperties的绑定
  • Mysql和Oracle的语法区别?
  • F - LIS on Tree
  • 2023 年全国大学生数学建模B题目-多波束测线问题
  • qt creater11 翻译国际化教程教程:
  • 【AWS实验 】在 AWS Fargate 上使用 Amazon ECS 部署应用程序
  • matlab几种求解器的选择fsolve-sole-vpasolve
  • 无限访问 GPT-4,OpenAI 强势推出 ChatGPT 企业版!
  • MySQL的故事——Schema与数据类型优化
  • C++编译和链接
  • 【CSDN技术】Markdown编辑器如何使用-csdn博客编写入门
  • 【docker】运行redis
  • Paddle训练COCO-stuff数据集学习记录
  • SpringBoot 框架学习
  • java - lua - redis 完成商品库存的删减
  • dbeaver离线安装clickhouse连接驱动
  • 2024腾讯校招后端面试真题汇总及其解答(二)
  • datagrip 相关数据连接信息无缝迁移
  • 不就是G2O嘛
  • C#开发的OpenRA游戏之系统参数选项按钮
  • 苹果启动2024年SRDP计划:邀请安全专家使用定制iPhone寻找漏洞
  • std::make_shared和new初始化智能指针的区别
  • 无涯教程-JavaScript - ERFC.PRECISE函数
  • 2023国赛数学建模C题思路分析 - 蔬菜类商品的自动定价与补货决策
  • 手写Spring:第1章-开篇介绍,手写Spring
  • C语言中,字节对齐是一种重要的内存管理概念
  • 网络丢包问题,敢不敢这样定位?
  • 【漏洞复现】H3C路由器信息泄露任意用户登录