【C++】常用查找算法
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法
//find//查找 内置数据类型
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){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 test02()
{vector<Person>v;//创建数据Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40); //放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);Person pp("bbb", 20);vector<Person>::iterator it = find(v.begin(), v.end(), p2);if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;}
}int main() {//test01();test02();system("pause");return 0;
}
//总结:利用find可以在容器中找到指定的元素,返回值是迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法 find_if//1、查找内置数据类型
class GreaterFive
{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(), GreaterFive());if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到大于5的数字为:" << *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 Greater20
{public:bool operator()(Person& p){return p.m_Age > 20;}
};void test02()
{vector<Person>v;//创建数据Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//找年龄大于20的人vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;}
}int main() {//test01();test02();system("pause");return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法 adjacent_findvoid test01()
{vector<int>v;v.push_back(0);v.push_back(2);v.push_back(0);v.push_back(4);v.push_back(1);v.push_back(1);vector<int>::iterator pos = adjacent_find(v.begin(), v.end());if (pos == v.end()){cout << "未找到相邻重复元素" << endl;}else{cout << "找到相邻重复元素:" << *pos << endl;}
}int main() {test01();system("pause");return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法 binary_searchvoid 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 << "找到了元素" << endl;}else{cout << "未找到" << endl;}}int main() {test01();system("pause");return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法count//1、统计内置数据类型
void test01()
{vector<int>v;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(40);int num = count(v.begin(), v.end(), 40);cout << "40的元素个数为:" << 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 (this->m_Age == p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};void test02()
{vector<Person>v;Person p1("刘备",35);Person p2("关羽", 35);Person p3("张飞", 35);Person p4("赵云", 35);Person p5("曹操", 35);//将人员插入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);Person p("诸葛亮", 35);int num = count(v.begin(), v.end(), p);cout << "和诸葛亮同岁数的人员个数为:" << num << endl;
}int main() {//test01();test02();system("pause");return 0;
}
//总结:统计自定义数据类型时候,需要配合重载operator==
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法 count_if//统计内置数据类型
class Greater20
{public:bool operator()(int val){return val > 20;}
};void test01()
{vector<int>v;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(20);v.push_back(40);int num = count_if(v.begin(), v.end(), Greater20());cout << "大于20的元素个数为:" << 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 AgeGreater20
{public:bool operator()(const Person& p){return p.m_Age > 20;}
};//统计自定义数据类型
void test02()
{vector<Person>v;Person p1("刘备", 35);Person p2("关羽", 35);Person p3("张飞", 35);Person p4("赵云", 40);Person p5("曹操", 20);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);//统计 大于20岁人员个数int num = count_if(v.begin(), v.end(), AgeGreater20());cout << "大于20岁的人员个数为:" << num << endl;
}int main() {//test01();test02();system("pause");return 0;
}
【C++】常用查找算法相关推荐
- C++ STL 常用查找算法
C++ STL 常用查找算法 adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. ...
- C++STL常用查找算法
C++STL常用查找算法 学习目标 算法简介 find 功能描述 函数原型 示例 总结 find_if 功能描述 函数原型 示例 总结 adjacent_find 功能描述 函数原型 示例 总结 bi ...
- STL算法——常用查找算法(find、find_if、adjacent_find、binary_search、count、count_if)
5.2 常用查找算法 学习目标: 掌握常用的查找算法 算法简介: find //查找元素 find_if //按条件查找元素 adjacent_find //查找相邻重复元素 binary_searc ...
- C++实现常用查找算法
在日常编程和面试中,查找算法和排序算法需要非常熟练.本文用C++语言的语法来写常用的查找算法:顺序查找,二分查找, 一.顺序查找 1.1基本思想(有序无序皆可以) 1 从表中的第一个元素开始,依次与关 ...
- C++常用查找算法总结(一)
查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找,字段的查找,等等. 1.查找算法总结 (1). 最容易理解的查找算法,顺序查找法 说明:顺 ...
- java 二分搜索获得大于目标数的第一位_程序员常用查找算法(顺序、二分、插值、分块、斐波那契)...
顺序查找 基本思想 属于线性查找和无序查找,从一端开始顺序扫描,直到找到与目标值value相等的元素. 这是最基本的查找方法,也是时间复杂度最高的查找算法. 在数据过多时,这种方法并不适用. 代码实现 ...
- c语言中的常用查找算法
1.顺序查找: 概念:按照顺序,从第一个元素遍历到最后一个元素,找到就返回元素下标:下面的代码可以帮助你更好的理解: int main {int arr[]={1,2,3,4,5,6,7};int k ...
- C++ 常用查找算法
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <algorithm> using namespace ...
- 常用查找算法之B/B+树
文章目录 前言 B- 树 查找 插入 删除 B+ 树 查找 范围查找 总结 参考资料 前言 从算法逻辑上讲二叉查找树的查找和插入操作效率都已经很高,但是在实际应用中由于我们不能将整个索引表加载到内存, ...
- 常用查找算法(顺序、折半、二叉树、哈希、分块)介绍
一.顺序查找 条件:无序或有序队列. 原理:按顺序比较每个元素,直到找到关键字为止. 时间复杂度:O(n) 二.二分查找(折半查找) 条件:有序数组 原理:查找过程从数组的中间元素开始,如果中间元 ...
最新文章
- linux的翻译系统开发,Linux系统下的翻译神器——Goldendict
- 聊一个不常见的面试题:为什么数据库连接池不采用 IO 多路复用?
- 大学学习方法介绍之:使用两套教材
- 高等数学上-赵立军-北京大学出版社-题解-练习2.3
- pycharm下载与安装
- 女性最常说的5大谎言:
- 什么是程序员的优良品质
- 深入了解preventDefault与stopPropagation
- 一生要做的九十九件事
- python模块之httplib(在py3中功能进一步强大,请详看文档)
- HTTP 500 - 内部服务器错误的解决
- 宜昌市计算机一级考试真题,宜昌市人事局关于在大中专院校学生中开展计算机应用等级考试的通知...
- 六、面向对象编程——类和对象
- 时间序列模型c语言,时间序列分析步骤及sas代码
- 论文清单:一文梳理因果推理在自然语言处理中的应用
- python实现免费同声传译 (离线语音识别+免费翻译接口+系统声音录制)
- strcpy()的使用注意事项
- 怎样在群晖中安装GitLab用于项目代码管理
- 如何检验计算机主板的好坏,怎么判断笔记本主板、CPU硬件好坏? 查看电脑配置的教程...
- 仿QQ浏览器mac版官网主页 html css3特效