本文共 1689 字,大约阅读时间需要 5 分钟。
c++find函数用法
头文件
#include
函数实现
复制代码
templateInputIterator find (InputIterator first, InputIterator last, const T& val){ while (first!=last) { if (*first==val) return first; ++first; } return last;}
复制代码
例1(vector)复制代码
#include#include #include using namespace std;int main(){ vector 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#include #include #include using namespace std;int main(){ set 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#include #include #include using namespace std;int main(){ set 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#include #include 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;}
转载地址:http://lpxgi.baihongyu.com/