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函数的用法的更多相关文章

  1. matlab中patch函数的用法

    http://blog.sina.com.cn/s/blog_707b64550100z1nz.html matlab中patch函数的用法——emily (2011-11-18 17:20:33) ...

  2. mysql中INSTR函数的用法

    mysql中INSTR函数的用法 INSTR(字段名, 字符串) 这个函数返回字符串在某一个字段的内容中的位置, 没有找到字符串返回0,否则返回位置(从1开始) SELECT * FROM tblTo ...

  3. (转)解析PHP中ob_start()函数的用法

    本篇文章是对PHP中ob_start()函数的用法进行了详细的分析介绍,需要的朋友参考下     ob_start()函数用于打开缓冲区,比如header()函数之前如果就有输出,包括回车/空格/换行 ...

  4. Delphi中 StrToIntDef函数的用法

    Delphi中 StrToIntDef函数的用法:比如我要判断一个文本框里输入的字符串能不能转换为integer类型,如果能,则返回转换后的整型数据,如果不能,则返回整数0,那么我就可以用strtoi ...

  5. Python中int()函数的用法浅析

      int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int)  Help on class int in module __builti ...

  6. matlab中repmat函数的用法(堆叠矩阵)

    matlab中repmat函数的用法 B = repmat(A,m,n) B = repmat(A,[m n]) B = repmat(A,[m n p...]) 这是一个处理大矩阵且内容有重复时使用 ...

  7. Oracle 中 CONTAINS 函数的用法

    Oracle 中 CONTAINS 函数的用法 1. 查询住址在北京的学生 SELECT student_id,student_name FROM students WHERE CONTAINS( a ...

  8. Matlab中imfilter()函数的用法

    Matlab中imfilter()函数的用法 功能:对任意类型数组或多维图像进行滤波.用法:B = imfilter(A,H) B = imfilter(A,H,option1,option2,... ...

  9. SQL中DATENAME函数的用法

    在SQL数据库中有多种函数,下面就将为您介绍其中的DATENAME函数的用法,供您参考,希望对您学习SQL中函数的用法能有所帮助. 在SQL数据库中,DATENAME函数的作用是是从日期中提取指定部分 ...

  10. python中open函数的用法

    用法如下: name = open('errname.txt','w')name.readline()name.close() 1.看下第一行的代码 用来访问磁盘中存放的文件,可以进行读写等操作,例如 ...

随机推荐

  1. 实施软件测试风险分析&回归用例刷选

    [一两年前收集整理的资料,感觉不错,放到博客上来] 作为软件测试计划的一部分,软件测试风险的分析与控制是其中重要的环节.如果前期风险分析与控制比较充分,那么会使软件的测试成功性大大增加,且可将由风险异 ...

  2. Windows查看端口使用状况

    使用端口是我们在进行远程或者打印机等都会遇到的,但是有很多用户会遇到端口被占用的情况,遇到这样的问题首先就要找出电脑中的所以端口然后进行查看,还是有很多人不知道该如何查看电脑端口. 1 查看windo ...

  3. Java 9 揭秘(19. 平台和JVM日志)

    Tips 做一个终身学习的人. 在这章中,主要介绍以下内容: 新的平台日志(logging)API JVM日志的命令行选项 JDK 9已经对平台类(JDK类)和JVM组件的日志系统进行了大整. 有一个 ...

  4. Python LeetCode

    Python不熟悉 不同的做法 404. Sum of Left Leaves 这是我的做法,AC. class Solution(object): res = 0 def recursive(sel ...

  5. 认真地搞OI

    新博客的开头 OI生涯的开始 #include<cstdio> int main() { puts("Hello world!"); ; }

  6. Android 原生 Intent 分享支持的那些事

    版权声明: 本账号发布文章均来自公众号,承香墨影(cxmyDev),版权归承香墨影所有. 每周会统一更新到这里,如果喜欢,可关注公众号获取最新文章. 未经允许,不得转载. 一.前言 对于一个 App ...

  7. 【思维】【水】 南阳oj 喷水装置(一)

    描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它为中心的半径为实数Ri(0<Ri<15)的圆被湿润,这有充足的喷水装置i(1& ...

  8. MySQL for Mac 安装和基本操作

    一.安装mysql 1.mysql下载地址http://dev.mysql.com/downloads/mysql/我的机器是mac 10.8的;所以使用mysql-5.6.10-osx10.7-x8 ...

  9. 揭秘Socket与底层数据传输实现

    揭秘socket 什么是socket?socket字面意思其实就是一个插口或者套接字,包含了源ip地址.源端口.目的ip地址和源端口.但是socket在那个位置呢 ,在TCP/IP网络的四层体系和OS ...

  10. location对象的使用

    Location 对象属性 属性 描述 hash 设置或返回从井号 (#) 开始的 URL(锚). host 设置或返回主机名和当前 URL 的端口号. hostname 设置或返回当前 URL 的主 ...