C++ string中的find()函數


1.string中find()返回值是字母在母串中的位置(下標記錄),如果沒有找到,那么會返回一個特別的標記npos。(返回值可以看成是一個int型的數)

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 using namespace std;
 5 int main()
 6 {
 7     ////find函數返回類型 size_type
 8     string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
 9     string flag;
10     string::size_type position;
11     //find 函數 返回jk 在s 中的下標位置
12     position = s.find("jk");
13     if (position != s.npos)  //如果沒找到,返回一個特別的標志c++中用npos表示,我這里npos取值是4294967295,
14     {
15         printf("position is : %d\n" ,position);
16     }
17     else
18     {
19         printf("Not found the flag\n");
20     }
21 }

 

2.返回子串出現在母串中的首次出現的位置,和最后一次出現的位置。

1  flag = "c";
2      position = s.find_first_of(flag);
3      printf("s.find_first_of(flag) is :%d\n",position);
4      position = s.find_last_of(flag);
5      printf("s.find_last_of(flag) is :%d\n",position);

 

 

3.查找某一給定位置后的子串的位置

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

 

4.查找所有子串在母串中出現的位置

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

 

5.反向查找子串在母串中出現的位置,通常我們可以這樣來使用,當正向查找與反向查找得到的位置不相同說明子串不唯一。

1     //反向查找,flag 在s 中最后出現的位置
2     flag="3";
3     position=s.rfind (flag);
4     printf("s.rfind (flag) :%d\n",position);

 

 

例題:

1.給出一個字符串,串中會出現有人名,找到一個只有一個人名的字符串。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 vector<string> s;
 4 int main()
 5 {
 6     s.push_back("Danil");
 7     s.push_back("Olya");
 8     s.push_back("Slava");
 9     s.push_back("Ann");
10     s.push_back("Nikita");///建立動態數組
11     string a;
12     cin>>a;
13     int res = 0;
14     for(int i = 0; i < 5; i++)
15     {
16         if(a.find(s[i]) != a.npos)
17         {
18             res++;
19             if(a.rfind(s[i]) != a.find(s[i]))///一個字符中出現多個一樣的名字
20             {
21                 res++;
22             }
23         }
24     }
25     if(res == 1) 
26     {
27         cout<<"YES"<<endl;
28     }
29     else 
30     {
31         cout<<"NO"<<endl;
32     }
33     return 0;
34 }

 

 

2.你有n個字符串。 每個字符串由小寫英文字母組成。 重新排序給定的字符串,使得對於每個字符串,在它之前的所有字符串都是它的子串。

https://www.cnblogs.com/wkfvawl/p/9229758.html

 1 #include<string>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<iostream>
 5 using namespace std;
 6 bool cmp(string a, string b)
 7 {
 8     if (a.length() == b.length())
 9         return a < b;
10     return a.length() < b.length();
11 }
12 int main()
13 {
14     int n;
15     string s[111];
16     scanf("%d", &n);
17     for (int i = 0; i < n; i++)
18     {
19         cin >> s[i];
20     }
21     sort(s, s + n, cmp);
22     int flag = 1;
23     for (int i = 1; i < n; i++)
24     {
25         if (s[i].find(s[i-1]) == string::npos)
26         {
27             flag = 0;
28             break;
29         }
30     }
31     if (flag)
32     {
33         cout << "YES" << endl;
34         for (int i = 0; i < n; i++)
35         {
36             cout << s[i] << endl;
37         }
38     }
39     else
40     {
41         cout << "NO" << endl;
42     }
43     return 0;
44 }

 

 3.查詢區間內子串在母串中的個數。

https://www.cnblogs.com/wkfvawl/p/9452869.html

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 int main()
 8 {
 9     int n,m,q,i,j,l,r,len;
10     int counts;
11     int vis[10010];
12     string s1,s2;
13     cin>>n>>m>>q;
14     cin>>s1>>s2;
15     len=s2.size();
16     memset(vis,0,sizeof(vis));
17     string::size_type pos=0;
18     while((pos=s1.find(s2,pos))!=string::npos)
19     {
20         vis[pos+1]=pos+1;
21         pos++;
22     }
23     for(i=1;i<=q;i++)
24     {
25         counts=0;
26         scanf("%d%d",&l,&r);
27         for(j=l;j<=r;j++)
28         {
29             if(vis[j]!=0&&vis[j]+len-1<=r)
30             {
31                counts++;
32             }
33         }
34         printf("%d\n",counts);
35     }
36     return 0;
37 }

 


免責聲明!

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



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