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

  • 相关阅读:
    Android图像格式类及图像转换方法
    Java实现文件的加密与解密
    Google最新截屏案例详解
    Android应用程序模拟手机按键
    Android浮动小球与开机自启动
    Android手机截屏
    Android图片浏览器之图片删除
    Android图片浏览器之缩略图
    MFC实现Gif动画制作工具
    QRadioButton分组且无边框的简单实现
  • 原文地址:https://www.cnblogs.com/VVingerfly/p/4583507.html
Copyright © 2011-2022 走看看