【C++】find()函数用法查找
1. 查找指向指定元素的迭代器
find函数主要实现的是在容器内查找指定的元素,并且这个元素必须是基本数据类型的。
查找成功返回一个指向指定元素的迭代器,查找失败返回end迭代器。
2. 在数组中查找:
# include <iostream>
# include <vector>
# include <algorithm> //注意要包含该头文件
using namespace std;
int main()
{int nums[] = { 3 , 1 , 4 , 1 , 5 , 9 };int num_to_find = 5 ;int start = 0 ;int end = 5 ;int * result = find( nums + start, nums + end, num_to_find );if ( result == nums + end ) {cout<< "Did not find any number matching " << num_to_find << endl;} else{cout<< "Found a matching number: " << *result << endl;}return 0 ;
}
3. 在容器中查找:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){vector< int > v;int num_to_find=25; //要查找的元素,类型要与vector<>类型一致for ( int i=0;i<10;i++)v.push_back(i*i);vector< int >::iterator iter=std::find(v.begin(),v.end(),num_to_find); //返回的是一个迭代器指针if (iter==v.end())cout<< "ERROR!" <<endl;else //注意迭代器指针输出元素的方式和distance用法cout<< "the index of value " <<(*iter)<< " is " << std::distance(v.begin(), iter)<<std::endl;return 0;
}
4. 函数find_if
find_if函数 带条件的查找元素,容器元素类型是类的时候,不能使用find函数,只能通过find_if函数来实现。find_if函数依次的遍历容器的元素,返回第一个使函数为true的元素的迭代器,如果查找失败则返回end迭代器
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template < typename T>
bool equal_3(T value){return value==3;
}
int main(){vector< int > vec;vec.push_back(7);vec.push_back(3);vec.push_back(8);vector< int >::iterator finda=find_if(vec.begin(),vec.end(),equal_3< int >);if (finda!=vec.end())cout<< "YES" <<*finda<<endl;elsecout<< "ERROR" <<endl;return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
struct Point
{int x;int y;
};
struct PointFindByCoord : public std::binary_function<Point, Point, bool >
{bool operator () ( const Point &obj1, const Point &obj2) const{return obj1.x == obj2.x && obj1.y == obj2.y;}
};
int main()
{std::vector<Point> v;for ( int i = 0; i < 5; ++i){for ( int j = 0; j < 5; ++j){Point pt;pt.x = i;pt.y = j;v.push_back(pt);}}Point needFind;needFind.x = 4;needFind.y = 3;std::vector<Point>::iterator iter=std::find_if(v.begin(),v.end(),std::bind2nd(PointFindByCoord(), needFind));if (iter == v.end()){// 未找到 }elsestd::cout << "the index of value Point(" << (*iter).x << ", " << (*iter).y<< ") is " << std::distance(v.begin(), iter) << std::endl;return 0;
}
5. 容器中逆向查找:
std::map反向遍历,for循环
#include <iostream>
#include <string>
#include <map>int main()
{std::map<int, std::string> t_Map;t_Map[0] = "A";t_Map[1] = "B";t_Map[2] = "C";std::map<int, std::string>::reverse_iterator iter1;for (iter1 = t_Map.rbegin();iter1 != t_Map.rend();iter1++){std::cout << iter1->first << " : " << iter1->second << std::endl;}getchar();return 0;
}
while循环
#include <iostream>
#include <string>
#include <map>int main()
{std::map<int, std::string> t_Map;t_Map[0] = "A";t_Map[1] = "B";t_Map[2] = "C";std::map<int, std::string>::reverse_iterator iter2 = t_Map.rbegin();while (iter2 != t_Map.rend()){std::cout << iter2->first << " : " << iter2->second << std::endl;iter2++;}getchar();return 0;
}
【C++】find()函数用法查找相关推荐
- python中类方法与实例方法的区别-Python中的对象,方法,类,实例,函数用法分析...
本文实例分析了Python中的对象,方法,类,实例,函数用法.分享给大家供大家参考.具体分析如下: Python是一个完全面向对象的语言.不仅实例是对象,类,函数,方法也都是对象. class Foo ...
- OVER(PARTITION BY)函数用法
OVER(PARTITION BY)函数介绍 开窗函数 Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返 ...
- 获得供应商最近一次报价:OVER(PARTITION BY)函数用法的实际用法
利用rownumber ,关键字partition进行小范围分页 方法一: --所有供应商对该产品最近的一次报价 with oa as ( select a.SupplierId ,UnitPrice ...
- php autoload用法,php自动加载__autoload()函数用法
autoload在php主版本中是自动加载文件的一个方法了我们可以定义许多的文件在目录然后通过autoload加载了,下面来看看autoload函数用法. 在PHP5之后,我们可以通过 __autol ...
- C++string类常用函数 c++中的string常用函数用法总结
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类 ...
- hog函数的用法 python_Python常见内置函数用法(三)
本文作者:孙雪丽 文字编辑:周聪聪 技术总编:张学人 重大通知!!!2019年6月22日至24日在河南郑州举行Stata编程技术特别训练营,招生工作已经圆满结束.另外爬虫俱乐部将于2019年7月5日至 ...
- python中的get函数_python之函数用法get()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法get() #http://www.runoob.com/python/att-dic ...
- string常用函数用法集合
写在前面的话: 我的字符串还是太弱了,记录一下string类型实用的一些函数用法. (1)find() 定义string 类型的 s和t 1.s.find(t):从s中找t,返回找到的第一个t的第一个 ...
- eval()函数和zip()函数用法、enumerate()
str():把字典.元组.列表转换为字符串 eval():字符串转换为字典.元组.列表 eval是Python的一个内置函数,这个函数的作用是,返回传入字符串的表达式的结果.即变量赋值时,等号右边的表 ...
最新文章
- ubuntu下使用pidgin(pidgin-lwqq)登陆qq
- Linux下进程通信的八种方法
- 可汉学院python_可汗学院离线版本
- Apache JMeter 启动
- 计算已知经纬度两点的距离_python
- 文件带 BOM 的看法
- Java 算法刷题指南
- 这 10 个 Python 可视化工具,你用过哪些?
- lenovo 笔记本ideapad 320c-15改装win7问题
- 信息化管理系统在企业的应用
- linux 关闭后台程序
- sublime text3怎么分屏显示及关闭分屏?
- 什么是cosmos?
- Halcon视觉检测——使用分类器分类
- 安庆集团-冲刺日志(第五天)
- 香港主机CDN加速,让您的网站速度飙升
- Windows下清理redis缓存
- Minio根据文件地址,返回一个供外网访问的地址
- Hawk-数据抓取工具:简明教程
- MCDF实验——Lab5
热门文章
- 企业即时通讯相关英文
- linux查看文件创建人,Linux中如何查看文件的创建时间详解
- 银行家算法( 详解 )
- 【python与数据分析】NumPy数值计算基础1——numpy数组及其运算
- uGUI之AutoLayout详解——minHeight,preferredHeight,flexibleHeight
- 【解救ROS】关于ros机器人(小车)动态调试PID参数
- Proteus仿真STM32的课设实例3——汽车倒车测距提示仪
- android模拟器在任务栏上,但是不能显示出窗口
- 《C++项目开发实战入门》攻坚战
- 新华三 H3C ER2100V2 路由器组网配置