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. 一步一步学Vue(四)

    接上篇.上篇中给出了代码框架,没有具体实现,这一篇会对上篇定义的几个组件进行分别介绍和完善: 1.TodoContainer组件 TodoContainer组件,用来组织其它组件,这是react中推荐 ...

  2. java 笔记 Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别

    查了一些资料也不是太明白两个的区别,但是前者是最安全的用法 打个简单的比方,你一个WEB程序,发布到Tomcat里面运行.首先是执行Tomcat org.apache.catalina.startup ...

  3. 6.RDD持久性

    RDD持久性 1 Why Apache Spark 2 关于Apache Spark 3 如何安装Apache Spark 4 Apache Spark的工作原理 5 spark弹性分布式数据集 6 ...

  4. OVS故障处理一例

    OVS下无法访问内部网站 遇到朋友求助的一个客户问题,环境是这样的,客户在自己的iaas平台(不是openstack)上使用ovs,物理交换机上配置vlan和dhcp service,计算节点的ovs ...

  5. 深入浅出数据结构C语言班(11)——简要介绍算法时间复杂度

    在接下来的数据结构博文中,我们将会开始接触到一些算法,也就是"解决某个问题的方法",而解决同一个问题总是会存在不同的算法,所以我们需要在不同的算法之中做出抉择,而做出抉择的根据往往 ...

  6. echarts_部分图表配置简介_横向柱状图

    横向柱状图主要配置x位置x轴类型y轴类型(轴的类型分两种 1.category(类别)2.value(值)),代码简单(里面有注释)效果如下: var myChart = echarts.init(d ...

  7. HttpURLConnection getInputStream异常的解决

    http://blog.csdn.net/q2232/article/details/48136973 但是当getResponseCode为自定义值,比如422时,httpURLConnection ...

  8. 刚装的系统C盘占空间特别大怎么办?关闭win7的系统还原和调整虚拟内存

    刚装的win7系统,C盘占空间特别大,清理垃圾,清理碎片之后还是特别大,检查一下,系统才20G,C盘却占用了40G,其实是没有关闭系统还原,再就是没有调整虚拟内存.win7旗舰版(其他版本也可能这样, ...

  9. C#线程(二)

    .cnblogs_code { background-color: #f5f5f5; font-family: Courier New !important; font-size: 12px !imp ...

  10. My first_leetcode_Rever Ingeter 数字翻转java实现(办法集合)

    7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321  Example2: x = -123, ...