• 字符串查找与类型转换(C/C++)


    字符串查找

    strchr,strrchr与strstr

    功能:对字符串中的单个字符进行查找

    //strchr 函数原型的一般格式
    char *strchr(const char *str, int c);

    它表示在字符串str中查找字符,返回字符c第一次在字符串str中出现的位置,如果未找到字符c,则返回NULL。也就是说, strchr 函数在字符串str中从前到后(或者称为从左到右)查找字符c,找到字符c第一次出现的位置就返回,返回值指向这个位置,如果找不到字符c就返回NULL。

    //strrchr 函数原型的一般格式
    char *strrchr(const char *str, int c);

    与 strchr 函数一样,它同样表示在字符串str中查找字符c,返回字符c第一次在字符串str中出现的位置,如果未找到字符c,则返回NULL。但两者唯一不同的是, strrchr 函数在字符串s中是从后到前(或者称为从右向左)查找字符c,找到字符c第一次出现的位置就返回,返回值指向这个位置。

    代码示例

    //strchr和strrchr函数使用演示
    int main(void)
    {
        char str[] = "I welcome any ideas from readers, of course.";
        char *lc = strchr(str, 'o');
        printf("strchr: %s
    ", lc);
        char *rc = strrchr(str, 'o');
        printf("strrchr: %s
    ", rc);
        return 0;
    }

    对于上面的示例代码, strchr 函数是按照从前到后的顺序进行查找,所以得到的结果为“ome any ideas from readers,of course.”; 而 strrchr 函数则相反,它按照从后到前的顺序进行查找,所以得到的结果为“ourse.”。

    //示例代码运行结果
    strchr: ome any ideas from readers, of course.
    strrchr: ourse.

    注意】函数的“c”参数是int类型,而不是char类型。这里用的是字符的ASCII 码(因为每个字符都对应着一个ASCII码),这样在传值的时候既可以传char类型的值,又可以传int类型的值(0~127)。

    string类中的find系列函数

    功能:在母串中查找子串

    find(str)

    返回值是子串在母串中的位置(下标记录,如果没有找到,那么会返回一个特别的标记npos(返回值可以看成是一个int型的数)。

    示例:

    #include<cstring>
    #include<cstdio>
    #include<iostream>
    using namespace std;
    int main()
    {
        ////find函数返回类型 size_type
        string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
        string flag;
        string::size_type position;
        //find 函数 返回jk 在s 中的下标位置
        position = s.find("jk");
        if (position != s.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
        {
            printf("position is : %d
    " ,position);
        }
        else
        {
            printf("Not found the flag
    ");
        }
    }

    find_first_of(str)和find_last_of(str)

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

    示例:

    flag = "c";
    position = s.find_first_of(flag);
    printf("s.find_first_of(flag) is :%d
    ",position); //5
    position = s.find_last_of(flag);
    printf("s.find_last_of(flag) is :%d
    ",position); //25

    find(str, pos)

    查找某一给定位置后的子串的位置。

    示例:

    //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
    position=s.find("b",5);
    cout<<"s.find(b,5) is : "<<position<<endl; //23
    //查找s 中flag 出现的所有位置。
    flag="a";
    position=0;
    int i=1;
    while((position=s.find(flag,position))!=string::npos) {
        cout<<"position  "<<i<<" : "<<position<<endl;
        position++;
        i++;
    }

    rfind()

    反向查找子串在母串中出现的位置。通常我们可以这样来使用,当正向查找与反向查找得到的位置不相同说明子串不唯一

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

    【find系列函数应用例题】

    1.给出一个字符串,串中会出现有人名,找到一个只有一个人名的字符串。

    #include <bits/stdc++.h>
    using namespace std;
    vector<string> s;
    int main()
    {
        s.push_back("Danil");
        s.push_back("Olya");
        s.push_back("Slava");
        s.push_back("Ann");
        s.push_back("Nikita");///建立动态数组
        string a;
        cin>>a;
        int res = 0;
        for(int i = 0; i < 5; i++)
        {
            if(a.find(s[i]) != a.npos)
            {
                res++;
                if(a.rfind(s[i]) != a.find(s[i]))///一个字符中出现多个一样的名字
                {
                    res++;
                }
            }
        }
        if(res == 1)
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
        return 0;
    }
    View Code

    2.你有n个字符串。 每个字符串由小写英文字母组成。 重新排序给定的字符串,使得对于每个字符串,在它之前的所有字符串都是它的子串。

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

    #include<string>
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    bool cmp(string a, string b)
    {
        if (a.length() == b.length())
            return a < b;
        return a.length() < b.length();
    }
    int main()
    {
        int n;
        string s[111];
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            cin >> s[i];
        }
        sort(s, s + n, cmp);
        int flag = 1;
        for (int i = 1; i < n; i++)
        {
            if (s[i].find(s[i-1]) == string::npos)
            {
                flag = 0;
                break;
            }
        }
        if (flag)
        {
            cout << "YES" << endl;
            for (int i = 0; i < n; i++)
            {
                cout << s[i] << endl;
            }
        }
        else
        {
            cout << "NO" << endl;
        }
        return 0;
    }
    View Code

    3.查询区间内子串在母串中的个数。

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

    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int n,m,q,i,j,l,r,len;
        int counts;
        int vis[10010];
        string s1,s2;
        cin>>n>>m>>q;
        cin>>s1>>s2;
        len=s2.size();
        memset(vis,0,sizeof(vis));
        string::size_type pos=0;
        while((pos=s1.find(s2,pos))!=string::npos)
        {
            vis[pos+1]=pos+1;
            pos++;
        }
        for(i=1;i<=q;i++)
        {
            counts=0;
            scanf("%d%d",&l,&r);
            for(j=l;j<=r;j++)
            {
                if(vis[j]!=0&&vis[j]+len-1<=r)
                {
                   counts++;
                }
            }
            printf("%d
    ",counts);
        }
        return 0;
    }
    View Code

    类型转换

    string类 <=> char数组

    char[] => string

    char ch [] = “ABCDEFG”;
    string str(ch); //也可string str = ch;
    //或者
    char ch [] = “ABCDEFG”;
    string str;
    str = ch; //在原有基础上添加可以用str += ch;

    string => char[]

    char buf[10];
    string str(“ABCDEFG”);
    length = str.copy(buf, 9);
    buf[length] = ‘0’;
    //或者
    char buf[10];
    string str(“ABCDEFG”);
    strcpy(buf, str.c_str()); //strncpy(buf, str.c_str(), 10);

    string类 <=> int整型

    int => string

    (1)to_string函数

    //c++11标准增加了全局函数std::to_string:
    string to_string (int val);
    string to_string (long val);
    string to_string (long long val);
    string to_string (unsigned val);
    string to_string (unsigned long val);
    string to_string (unsigned long long val);
    string to_string (float val);
    string to_string (double val);
    string to_string (long double val);

    示例:

    // to_string example
    #include <iostream>   // std::cout
    #include <string>     // std::string, std::to_string
    
    int main ()
    {
      std::string pi = "pi is " + std::to_string(3.1415926);
      std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
      std::cout << pi << '
    ';
      std::cout << perfect << '
    ';
      return 0;
    }
    Output
    pi is 3.141593
    28 is a perfect number
    to_string example
    //实现to_string函数
    #include<iostream>
    #include<string>
    using namespace std;
    #define max 100
    string to_String(int n)
    {
        int m = n;
        char s[max];
        char ss[max];
        int i=0,j=0;
        if (n < 0)// 处理负数
        {
            m = 0 - m;
            j = 1;
            ss[0] = '-';
        }
        while (m>0)
        {
            s[i++] = m % 10 + '0';
            m /= 10;
        }
        s[i] = '';
        i = i - 1;
        while (i >= 0)
        {
            ss[j++] = s[i--];
        }
        ss[j] = '';
        return ss;
    }
    
    int main()
    {
        cout << "请输入整数:";
        int m;
        cin >> m;
        string s = to_String(m) + "abc";
        cout << s << endl;
        system("pause");
        return 0;
    }
    to_string函数实现

    (2)字符串流stringstream

    标准库定义了三种类型字符串流:istringstream、ostringstream以及stringstream,看名字就知道这几种类型和iostream中的几个非常类似,分别可以读、写以及读和写string类型,它们也确实是从iostream类型派生而来的。要使用它们需要包含sstream头文件。

    除了从iostream继承来的操作,

    1. sstream类型定义了一个有string形参的构造函数,即:stringstream stream(s);  ,创建了存储s副本的stringstream对象,s为string类型对象。
    2. 定义了名为str的成员,用来读取或设置stringstream对象所操纵的string值: 
      • string s = stream.str();  返回stream中存储的string类型对象s;
      • stream.str(s);  将string类型的s复制给stream,返回void;

    示例:

    int aa = 30;
    stringstream ss;
    ss<<aa; 
    string s1 = ss.str();
    cout<<s1<<endl; // 30

    string => int

    (1)采用标准库中atoi函数,对于其他类型也都有相应的标准库函数,比如浮点型atof(),long型atol()等等。

    示例:

    std::string str = "123";
    int n = atoi(str.c_str());
    cout<<n; //123
    //附:atoi()函数的源码实现
    //这个函数的主要功能是将一个字符串转化为一个数字,可能第一眼看上去,你会觉得这是一个很简单的函数,
    //甚至是一个不需要多少行代码就可以实现的函数。其实这是一个看着很简单,但是实践起来还有些需要注意的地方。
    //总的来说,有以下5种情况:
    //1--指针为NULL
    //2--空字符处理
    //3--正号与负号的处理
    //4--溢出处理
    //5--如果遇到异常字符怎么处理
    
    #include<iostream>
    enum ret { kvalid=0,kinvalid };    // 是否有非法输入的标记
    int status = kvalid;
    long long Strtointcode(const char* digit, bool minus)
    {
        long long num = 0;
        while (*digit != '')
        {
            if (*digit >= '0'&&*digit <= '9')
            {
                int flag = minus ? -1 : 1;
                num = num * 10 + flag*(*digit - '0');
                if ((!minus&&num > 0x7FFFFFFF) ||( minus&&num < (signed int)0x80000000))
                {
                    num = 0;
                    break;
                }
                digit++;
            }
            else
            {
                num = 0;
                break;
            }
        }
        if (*digit == '')
            status = kvalid;
        return num;
    }
    int Strtoint(const char* str)
    {
        status = kinvalid;
        long long num = 0;
        if (str != NULL&&*str != '')
        {
            bool minus = false;
            if (*str == '+')
                str++;
            else if (*str == '-')
            {
                str++;
                minus = true;
            }
            if (*str != '')
                num = Strtointcode(str, minus);
                
        }
        return (int)num;
    }
    
    int main()
    {
        char arr[20];
        int ret = 0;
        printf("请输入您要转化的字符串:
    ");
        scanf("%s", arr);
        ret = Strtoint(arr);
        if (kvalid == status)
        {
            printf("%d
    ", ret);
        }
        else
        {
            printf("输入非法
    ");
            printf("%d
    ", ret);
        }
        system("pause");
        return 0;
    }

    (2)采用sstream头文件中定义的字符串流对象来实现转换

    istringstream is("12"); //构造输入字符串流,流的内容初始化为“12”的字符串   
    int i;   
    is >> i; //从is流中读入一个int整数存入i中

    (整理自网络)

    参考资料:

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

    https://www.cnblogs.com/smile233/p/8379802.html

    Min是清明的茗
  • 相关阅读:
    Android Studio --“Cannot resolve symbol” 解决办法
    js与android webview交互
    关于post与get请求参数存在特殊字符问题
    Fragment 学习笔记(1)
    Android Studio 错误集
    UVA
    UVA
    UVALive
    考试题string——线段树。
    洛谷 1552 [APIO2012]派遣
  • 原文地址:https://www.cnblogs.com/MinPage/p/13941862.html
Copyright © 2020-2023  润新知