c++中find函数的用法
find函数主要实现的是在容器内查找指定的元素,并且这个元素必须是基本数据类型的。
查找成功返回一个指向指定元素的迭代器,即元素在容器中的下标,查找失败返回end迭代器。
头文件
#include <algorithm>
函数实现
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last)
{
if (*first==val) return first;
++first;
}
return last;
}
例1(vector)

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main()
{
vector<string> m;
m.push_back("hello");
m.push_back("hello2");
m.push_back("hello3");
if (find(m.begin(), m.end(), "hello") == m.end())
cout << "no" << endl;
else
cout << "yes" << endl;
}

例2(set)

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std; int main()
{
set<string> m;
m.insert("hello");
m.insert("hello2");
m.insert("hello3");
if (find(m.begin(), m.end(), "hello") == m.end())
cout << "no" << endl;
else
cout << "yes" << endl;
}

注1:set自身有个find函数,举例如下:

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std; int main()
{
set<string> m;
m.insert("hello");
m.insert("hello2");
m.insert("hello3");
if (find(m.begin(), m.end(), "hello") == m.end())
cout << "no" << endl;
else
cout << "yes" << endl;
}

注2:string自身有个find函数,没有找到返回-1,举例如下:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std; int main()
{
string s = "helllo";
if (s.find("e") != -1) //yes
cout << "yes" << endl;
else
cout << "no" << endl; if (s.find("z") != -1) //no
cout << "yes" << endl;
else
cout << "no" << endl;
}
c++中find函数的用法的更多相关文章
- matlab中patch函数的用法
http://blog.sina.com.cn/s/blog_707b64550100z1nz.html matlab中patch函数的用法——emily (2011-11-18 17:20:33) ...
- mysql中INSTR函数的用法
mysql中INSTR函数的用法 INSTR(字段名, 字符串) 这个函数返回字符串在某一个字段的内容中的位置, 没有找到字符串返回0,否则返回位置(从1开始) SELECT * FROM tblTo ...
- (转)解析PHP中ob_start()函数的用法
本篇文章是对PHP中ob_start()函数的用法进行了详细的分析介绍,需要的朋友参考下 ob_start()函数用于打开缓冲区,比如header()函数之前如果就有输出,包括回车/空格/换行 ...
- Delphi中 StrToIntDef函数的用法
Delphi中 StrToIntDef函数的用法:比如我要判断一个文本框里输入的字符串能不能转换为integer类型,如果能,则返回转换后的整型数据,如果不能,则返回整数0,那么我就可以用strtoi ...
- Python中int()函数的用法浅析
int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int) Help on class int in module __builti ...
- matlab中repmat函数的用法(堆叠矩阵)
matlab中repmat函数的用法 B = repmat(A,m,n) B = repmat(A,[m n]) B = repmat(A,[m n p...]) 这是一个处理大矩阵且内容有重复时使用 ...
- Oracle 中 CONTAINS 函数的用法
Oracle 中 CONTAINS 函数的用法 1. 查询住址在北京的学生 SELECT student_id,student_name FROM students WHERE CONTAINS( a ...
- Matlab中imfilter()函数的用法
Matlab中imfilter()函数的用法 功能:对任意类型数组或多维图像进行滤波.用法:B = imfilter(A,H) B = imfilter(A,H,option1,option2,... ...
- SQL中DATENAME函数的用法
在SQL数据库中有多种函数,下面就将为您介绍其中的DATENAME函数的用法,供您参考,希望对您学习SQL中函数的用法能有所帮助. 在SQL数据库中,DATENAME函数的作用是是从日期中提取指定部分 ...
- python中open函数的用法
用法如下: name = open('errname.txt','w')name.readline()name.close() 1.看下第一行的代码 用来访问磁盘中存放的文件,可以进行读写等操作,例如 ...
随机推荐
- Python面向对象编程(二)
1.继承与派生 上文我们已经说过,Python中一切皆对象.我们从对象中抽取了共同特征和技能,得到了类的概念.类与类之间也有共同特征,我们可以从有共同特征和技能的类中提取共同的技能和特征,叫做父类. ...
- luogu P1361 小猫爬山 [iddfs]
题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了. WD和LHX只好花钱让它们坐索道下山.索道上的缆车最大承重量为W ...
- .Net中关于相等的问题
在.Net框架中,如果您查看所有类型的的基类:System.Object类,将找到如下4个与相等判断的方法: static Equals() virtual Equals() static Refer ...
- 登录界面Demo
今天记载一个Demo,这个是我练习项目中用到,供新手看看,界面图:
- 一份关于npm的新手指南
Node.js使得在服务器端使用JavaScript编写应用程序成为可能.它是基于V8Javascript运行时并且使用C++编写的,所以它的速度很快.最初,它旨在作为应用程序的服务器环境,但是开发人 ...
- 控制反转(IoC)-解析与实现
控制反转(Inversion of Control)缩写:IoC是面向对象编程中框架级别里的一个重要的概念, 可以说Spring框架的核心就是基于IoC原理的. 这个概念到底是什么呢? 这么讲吧,一个 ...
- 前端程序员经常忽视的一个JavaScript面试题
在网上找到一个有关JavaScript的面试题,特整理如下: function Foo() { getName = function () { alert (1); }; return this; } ...
- echarts_部分图表配置_堆叠折线图
echarts基本图表使用: 1.获取包裹元素(var myChart = echarts.init(document.getElementById('thisId'));)2.设置option(op ...
- AVR开发 Arduino方法(一) 端口子系统
Arduino UNO R3使用的主处理器ATMega328P上有3个8位的输入/输出端口,它们分别是PB,PC和PD.Arduino IDE提供的Blink示例可以帮助我们了解端口的数字输出功能: ...
- zabbix分布式监控的部署
zabbix是一个分布式监视,管理系统,基于server-clinet架构,可用于监视各种网络服务,服务器和网络机器等状态. server端基于C语言,web管理端Frontend则是基于PHPA制作 ...