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函數,舉例如下:

2:string自身有個find函數,舉例如下:

 

 1 int main()
 2 {
 3     string s = "helllo";
 4     if (s.find("e") == string::npos)  //yes
 5         cout << "no" << endl;
 6     else
 7         cout << "yes" << endl;
 8 
 9     if (s.find("z") == string::npos)  //no
10         cout << "no" << endl;
11     else
12         cout << "yes" << endl;
13 }
 
        
//find函數返回類型 size_type  

    string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");  
    string flag;  
    string::size_type position;  
    //find 函數 返回jk 在s 中的下標位置   
    position = s.find("jk");  
    if (position != s.npos)  //如果沒找到,返回一個特別的標志c++中用npos表示,我這里npos取值是4294967295,  
     {  
      cout << "position is : " << position << endl;  
     }  
    else  
     {  
      cout << "Not found the flag" + flag;  
     }

  

    //find 函數 返回flag 中任意字符 在s 中第一次出現的下標位置  
     flag = "c";  
     position = s.find_first_of(flag);  
     cout << "s.find_first_of(flag) is : " << position << endl;  

 

//從字符串s 下標5開始,查找字符串b ,返回b 在s 中的下標  
position=s.find("b",5);  
cout<<"s.find(b,5) is : "<<position<<endl; 
 
//find 函數 返回flag 中任意字符 在s 中第一次出現的下標位置  
 flag = "c";  
 position = s.find_first_of(flag);  
 cout << "s.find_first_of(flag) is : " << position << endl; 

 

    //從字符串s 下標5開始,查找字符串b ,返回b 在s 中的下標  
    position=s.find("b",5);  
    cout<<"s.find(b,5) is : "<<position<<endl;  

 

//查找s 中flag 出現的所有位置。  
 flag="a";  
 position=0;  
 int i=1;  
 while((position=s.find_first_of(flag,position))!=string::npos)  
 {  
  //position=s.find_first_of(flag,position);  
  cout<<"position  "<<i<<" : "<<position<<endl;  
  position++;  
  i++;  
 } 

 

//查找flag 中與s 第一個不匹配的位置  
flag="acb12389efgxyz789";  
position=flag.find_first_not_of (s);  
cout<<"flag.find_first_not_of (s) :"<<position<<endl; 

 

//反向查找,flag 在s 中最后出現的位置  
 flag="3";  
 position=s.rfind (flag);  
 cout<<"s.rfind (flag) :"<<position<<endl;  
}

 


 說明:

1.  如果string sub = ”abc“;

              string s = ”cdeabcigld“;

     s.find(sub) , s.rfind(sub) 這兩個函數,如果完全匹配,才返回匹配的索引,即:當s中含有abc三個連續的字母時,才返回當前索引。

     s.find_first_of(sub),   s.find_first_not_of(sub),   s.find_last_of(sub),  s.find_last_not_of(sub)  這四個函數,查找s中含有sub中任意字母的索引。

2.  如果沒有查詢到,則返回string::npos,這是一個很大的數,其值不需要知道。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM