zoukankan      html  css  js  c++  java
  • c++find函数用法

    头文件

    #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;
    }
  • 相关阅读:
    Uploadify跨域上传原理
    C#中HttpClient使用注意:预热与长连接
    前端必读:浏览器内部工作原理
    从零开始学习jQuery
    ManualResetEvent 类的用法
    线程池用法的改进
    我的第一篇博客
    Es6新语法 let篇
    如何测试解析是否生效?
    主机记录和记录值(域名服务器绑定详解)
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/4072901.html
Copyright © 2011-2022 走看看