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;
    }

    转载自 jihite

  • 相关阅读:
    CC++ 文件操作
    loadrunner之Paramater在负载测试中的数据生成规则
    loadrunner关联及web_reg_save_param方法浅析
    mysql union 与 union all 语法及用法
    sql 语句中as的用法和作用
    数据库主从复制和读写分离
    《剑指offer》算法题第十二天
    《剑指offer》算法题第十一天
    《剑指offer》算法题第十天
    《剑指offer》算法题第九天
  • 原文地址:https://www.cnblogs.com/VVingerfly/p/4583507.html
Copyright © 2011-2022 走看看