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;
    }
  • 相关阅读:
    第9章 使用ssh服务管理远程主机。
    Linux下的网络管理工具—OpenNMS
    第8章 Iptables与Firewalld防火墙
    Linux下更好用的帮助命令—cheat
    第7章 使用RAID与LVM磁盘阵列技术
    收藏的博客
    linux下vi编辑器常用命令
    搜索引擎高级使用技巧
    七牛云配置二级域名
    软考-系统架构师备考知识(一)
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/4072901.html
Copyright © 2011-2022 走看看