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

97.STL-查找算法 find

目录

STL-查找算法find

1.基本用法:

2.查找自定义类型:

3.查找范围:


STL-查找算法find

在C++的STL(标准模板库)中,find 算法用于在指定范围内查找指定值的元素。

功能描述:

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

函数原型:

  • find(iterator beg, iterator end, value);
  • // 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
  • // beg 开始迭代器
  • // end 结束迭代器
  • // value 查找的元素

以下是使用 find 算法的一些基本示例:

1.基本用法:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;int main() {vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };// 使用 find 查找数字 5vector<int>::iterator it = find(numbers.begin(), numbers.end(), 5);if (it != numbers.end()) {cout << "找到了:" << *it << endl;}else {cout << "未找到" << endl;}return 0;
}

2.查找自定义类型:

如果你在一个包含自定义类型的容器中查找元素,需要确保自定义类型有相应的比较方式,通常通过重载 == 运算符。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;class Person {
public:string name;int age;Person() = default;Person(string n,int a):name(n),age(a){}bool operator==(const Person& p) {return name == p.name && age == p.age;}void print_person()const {cout << "name:" << name << " age:" << age;}
};void test02() {vector<Person> p;p.push_back(Person("xiaobai", 11));p.push_back(Person("xiaohong", 12));p.push_back(Person("xiaocheng", 13));p.push_back(Person("xiaohuang", 10));p.push_back(Person("xiaolv", 9));p.push_back(Person("xiaoqing", 12));p.push_back(Person("xiaolan", 11));p.push_back(Person("xiaozi", 10));vector<Person>::iterator it=find(p.begin(), p.end(), Person("xiaolv", 9));if (it == p.end()) {cout << "没有找到这个人" << endl;}else {cout << "找到了这个人"  << endl;it->print_person();}
}
int main() {test02();return 0;
}

 

 这里的 Person 类重载了 == 运算符,以便在查找时进行比较。

3.查找范围:

你可以指定查找的范围,而不是整个容器。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;int main() {vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };// 查找范围在前半部分vector<int>::iterator it = find(numbers.begin(), numbers.begin() + 5, 3);if (it != numbers.end()) {cout << "找到了:" << *it << endl;}else {cout << "未找到" << endl;}return 0;
}

 写在最后:以上就是本篇文章的内容了,感谢你的阅读。如果感到有所收获的话可以给博主点一个赞哦。如果文章内容有遗漏或者错误的地方欢迎私信博主或者在评论区指出~   

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

相关文章:

  • 如何应对雨天飞行的挑战?无人机机库防护能力解析
  • 机器学习笔记 - 3D数据的常见表示方式
  • 【Node.js】解决npm报错:RequestError: unable to verify the first certificate
  • 语言模型文本处理基石:Tokenizer简明概述
  • 淘宝商品详情数据接口(店铺搬家、数据分析、代购商城、ERP选品、无货源铺货、品牌监控)
  • 面试篇之微服务(一)
  • 智慧科研助力科研数据的分析处理
  • el-select实现分屏效果
  • 微信小程序本地和真机调试文件上传成功但体验版不成功
  • windows系统用nginx部署web应用
  • 如何利用Python进行数据归一化?
  • Linux 基本语句_13_消息队列
  • Maven——仓库
  • Pandas:一个实用高效的Python数据处理库
  • Spring第三课,Lombok工具包下载,对应图书管理系统列表和登录界面的后端代码,分层思想
  • DDoS高防IP到底是什么?
  • el-row错位问题解决
  • torch indices x[indices] 内存不足崩溃,python进程锁报错。
  • 第二证券:机构争分夺秒抢滩 金融大模型落地为时尚早
  • C#WPF使用MaterialDesign 显示带遮罩的对话框
  • Nuxt.js:下一代Web开发框架的革命性力量
  • 【JavaEE初阶】死锁问题
  • uniapp 打包的 IOS打开白屏 uniapp打包页面空白
  • 在 Redis 中使用 JSON 文档:命令行界面(CLI)和 Navicat 集成
  • Win Server 2019远程桌面服务部署
  • vue3-在自定义hooks使用useRouter 报错问题
  • 深度学习框架:Pytorch与Keras的区别与使用方法
  • 1145. 北极通讯网络(Kruskal,并查集维护)
  • 【23-24 秋学期】NNDL 作业9 RNN - SRN
  • Docker + Jenkins + Nginx实现前端自动化部署