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

【c++】STL常用算法2—常用查找算法

文章目录

  • 常用查找算法
    • find
    • find_if
    • adjacent_find
    • binary_search
    • count
    • count_if


常用查找算法

算法简介:

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

find

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

函数原型:

find(iterator beg,iterator end,value);
//beg开始迭代器
//end结束迭代器
//value查找的元素
#include<iostream>
using namespace std;
#include<vector>//查找内置数据类型
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", 18);person p2("bbb", 19);person p3("ccc", 20);person p4("ddd", 21);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//查找p2这个人是否存在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_if

功能:按条件查找元素,找到返回指定元素的迭代器,找不到返回结束迭代器位置end()。

函数原型:

find_if(iterator beg,iterator end,_Pred);
//beg开始迭代器
//end结束迭代器
//_Pred函数或者谓词(返回bool类型的仿函数)
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>//查找内置数据类型
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);}//查找容器中是否有大于5的vector<int>::iterator it = find_if(v.begin(), v.end(), greater5());if (it == v.end()){cout << "没有找到!" << endl;}else{cout << "找到大于5的数为:" << *it << endl;//6}
}//查找自定义数据类型
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", 18);person p2("bbb", 19);person p3("ccc", 20);person p4("ddd", 21);//放入到容器中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;
}

adjacent_find

功能:查找相邻重复元素,返回相邻元素的第一个元素的迭代器。

函数原型:

adjacent_find(iterator beg,iterator end);
//beg开始迭代器
//end结束迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>void test01()
{vector<int> v;v.push_back(0);v.push_back(2);v.push_back(3);v.push_back(0);v.push_back(2);v.push_back(1);v.push_back(1);v.push_back(4);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;
}

binary_search

功能:查找指定元素是否存在,查到返回true,否则返回false,二分查找法查找效率很高。

注意:在无序序列中不可用。

函数原型:

bool binary_search(iterator beg,iterator value);
//beg开始迭代器
//end结束迭代器
//value查找的元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>//binary_search查找元素必须是有序序列
void test01()
{vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}v.push_back(5);//尾部插入5这个元素,变为无序序列,binary_search查找不到元素//查找容器中是否有9这个元素bool ret = binary_search(v.begin(), v.end(), 9);if (ret){cout << "找到元素" << endl;}else{cout << "未找到元素" << endl;}
}int main()
{test01();system("pause");return 0;
}

count

功能:统计元素个数。

函数原型:

count(iterator beg,iterator end,value);
//beg开始迭代器
//end结束迭代器
//value统计的元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>//统计内置数据类型
void test01()
{vector<int>v;v.push_back(1);	v.push_back(2);v.push_back(5);v.push_back(7);v.push_back(8);v.push_back(2);v.push_back(5);v.push_back(2);//统计5的个数int num = count(v.begin(), v.end(), 5);cout << "5的个数为:" << num << endl;
}//统计自定义数据类型
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("aaa", 18);person p2("bbb", 19);person p3("ccc", 18);person p4("ddd", 21);person p("ggg", 18);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//统计容器中与p年龄相同的人int num = count(v.begin(), v.end(), p);cout << "和ggg年龄相同的人数:" << num << endl;}int main()
{test01();test02();system("pause");return 0;
}

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

count_if

功能:按提条件统计元素个数。

函数原型:

count_if(iterator beg,iterator end,_Pred);
//beg开始迭代器
//end结束迭代器
//_Pred谓词
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>//统计内置数据类型
class greater5
{
public:bool operator()(int val){return val > 5;}
};void test01()
{vector<int>v;v.push_back(1);v.push_back(2);v.push_back(5);v.push_back(7);v.push_back(8);v.push_back(2);v.push_back(5);v.push_back(2);//统计5的个数int num = count_if(v.begin(), v.end(), greater5());cout << "大于5的个数为:" << 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 greater20
{
public:bool operator()(const person& p){return p.m_age > 20;//如果年龄大于20返回真,否则返回假}
};void test02()
{vector<person> v;//创建数据person p1("aaa", 23);person p2("bbb", 19);person p3("ccc", 20);person p4("ddd", 21);person p("ggg", 18);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//统计容器中与p年龄相同的人int num = count_if(v.begin(), v.end(), greater20());cout << "年龄大于20的人数:" << num << endl;
}int main()
{test01();test02();system("pause");return 0;
}

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

相关文章:

  • 史上最全最详细的Java架构师成长路径图,程序员必备
  • 第五章 事务管理
  • Redis:主从同步
  • Unity Animator.Play(stateName, layer, normalizedTime) 播放动画函数用法
  • python学习——【第三弹】
  • 科技云报道:AI大模型背后,竟是惊人的碳排放
  • 如何根据实际需求选择合适的三维实景建模方式?
  • CENTO OS上的网络安全工具(十八)ClickHouse及编程环境部署
  • Java中class文件的格式
  • C++排序算法
  • JAVA后端部署项目三步走
  • php使用zookeeper实现分布式锁
  • 力扣-可回收且低脂的产品
  • 代码随想录刷题-数组-二分查找
  • HCIA复习1
  • Kotlin中的destructuring解构声明
  • Kubernetes Pod 水平自动伸缩(HPA)
  • 钉钉、企业微信和飞书向“钱”看
  • 网上购物网站的设计
  • 【Java学习笔记】8.Java 运算符
  • RHCSA-使用命令管理文件(3.6)
  • socket聊天室--socket的建立
  • Raft图文详解
  • 春季出游,学会这些功能,让你旅途更舒心
  • 【华为OD机试真题java、python、c++、jsNode】简单的自动曝光【2022 Q4 100分】(100%通过)
  • react学习笔记-1:创建项目
  • vulnhub five86-2
  • OpenCV入门(四)快速学会OpenCV3画基本图形
  • 【MAC OS 命令行】Redis的安装、启动和停止。就是如此简单
  • Leetecode 661. 图片平滑器