zoukankan      html  css  js  c++  java
  • stl 库里的find函数用法

    转载自大佬:https://www.cnblogs.com/wkfvawl/p/9452869.html

    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
    " ,position);
    16     }
    17     else
    18     {
    19         printf("Not found the flag
    ");
    20     }
    21 }
    复制代码

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

    1  flag = "c";
    2      position = s.find_first_of(flag);
    3      printf("s.find_first_of(flag) is :%d
    ",position);
    4      position = s.find_last_of(flag);
    5      printf("s.find_last_of(flag) is :%d
    ",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_first_of(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
    ",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 }
    复制代码
  • 相关阅读:
    LightOJ 1132 Summing up Powers(矩阵快速幂)
    hdu 3804 Query on a tree (树链剖分+线段树)
    LightOJ 1052 String Growth && uva 12045 Fun with Strings (矩阵快速幂)
    uva 12304 2D Geometry 110 in 1! (Geometry)
    LA 3263 That Nice Euler Circuit (2D Geometry)
    2013 SCAUCPC Summary
    poj 3321 Apple Tree (Binary Index Tree)
    uva 11796 Dog Distance (几何+模拟)
    uva 11178 Morley's Theorem (2D Geometry)
    动手动脑
  • 原文地址:https://www.cnblogs.com/bhd123/p/9453662.html
Copyright © 2011-2022 走看看