C++ find 函数用法
2015-06-17 15:40
405 查看
头文件
#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函数,举例如下:
#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string s = "helllo"; if (s.find("e") == string::npos) //yes cout << "no" << endl; else cout << "yes" << endl; if (s.find("z") == string::npos) //no cout << "no" << endl; else cout << "yes" << endl; }
转载自 jihite
相关文章推荐
- C++命名空间 namespace的作用和使用解析
- POJ 1581 A Contesting Decision
- 对比文件MD5差异并保存
- 条款05:了解C++默默编写并调用哪些函数
- C++11 标准新特性: 右值引用与转移语义(点评)
- How to Properly Write Received UDP Audio Data to ALSA with C++
- C++ 12.1 类的定义、声明、成员函数、数据成员
- 《The Design and evolution of C++》读书笔记1
- 收藏:c语言的多线程同步
- C语言sscanf的用法总汇
- LeetCode 200. Number of Islands
- C++11 标准新特性: 右值引用与转移语义
- C/C++ char和int的区别
- C++函数模板使用中的注意事项
- 学长们的求职血泪史(C/C++/JAVA)
- VC++在一个类中调用另一个类中的函数
- 精确度问题
- PAT分支-15. 日K蜡烛图(15) C语言作答
- c++_单实例模式
- 请输出以下4*5矩阵