zoukankan      html  css  js  c++  java
  • 一文读懂C++ String类在算法竞赛中的常见用法

    一文读懂C++ String类在算法竞赛中的常见用法

    string 相较于C语言的字符数组可方便太多了,在算法竞赛中能大大节省我们的时间。以下是我在刷题中会使用到的常见String用法。注释都写好了。

    #include <iostream>
    #include <string>
    using namespace std;
    int main(){
        //1、字符串拼接
        string s1 = "Hello";
        string s2 = "World!";
        string s3 = s1 + s2;
        cout<< s3 <<endl; //输出为HelloWorld!
        s3.append("123"); //字符串自加
        cout<< s3 <<endl; //输出为HelloWorld!123
    
        //2、字符串输入输出
    //    cin >> s3; //输入Hello World! cin是以空格,回车作为结束输入的标志
    //    cout<< s3 <<endl; //输出为Hello
    
        //3、读取一行字符
    //    getline(cin, s3); // 读取⼀⾏的字符串,包括空格 输入Hello World!
        cout<< s3 <<endl; //输出为Hello World!
    
        //4、获取字符串的长度
        int lens1 = s1.length();
        cout<< lens1 <<endl; //输出为5
        int lens2 = s2.size();
        cout<< lens2 <<endl; //输出为6
    
    
        //5、将字符串当成字符数组使用
        cout<<s1[0]<<endl; //输出为H
        cout<<s1[1]<<endl; //输出为e
        cout<<s1[2]<<endl; //输出为l
    
        //6、字符串与字符数组的互换
        //字符串转字符数组
        //方法1
        char a[100];
        int lens = s1.copy(a,s1.size());
        a[lens] = '';
        printf("%s
    ",a); //输出Hello
        //方法2
        strcpy(a,s2.c_str());
        printf("%s
    ",a); //输出World!
    
        //字符数组转字符串
        string s5(a);
        cout<<s5<<endl; //输出World!
    
        //7、字符串的比较
        s1 = "abc";
        s2 = "abb";
        if (s1 > s2) cout<<"yes1"<<endl; //输出yes1 按照字典序比大小
        s1 = "Abc";
        s2 = "abb";
        if (s1 > s2) cout<<"yes2"<<endl; //不输出,大小写敏感,且大写字母的值小于小写字母的值 按照字典序比大小
    
        s1 = "A";
        s2 = "a";
        if (s1 < s2) cout<<"yes3"<<endl; //输出yes3 大写字母的值小于小写字母的值
    
        //8、字符串的子串
        s1 = "hello World!";
        s2 = s1.substr(2,5);
        cout<<s2<<endl; //输出 llo W 从下标2开始 截取5个字符
        s2 = s1.substr(3);
        cout<<s2<<endl; //输出 llo W 从下标3开始 截取3到末尾的所有字符
    
        //9、查找字符串的子串
        int n = s1.find("World");
        cout<<n<<endl; //输出6 即从下标6开始就是这个匹配的字符
        cout<<s1.find("o")<<endl; //输出4 从前往后查找输出最先匹配到的下标
        cout<<s1.rfind("o")<<endl; //输出7 从后往前查找输出最先匹配到的下标
        cout<<s1.find("oo")<<endl; //输出18446744073709551615 额,反正就是没有找到
    
        //10、替换字符串
        s1 = "hello World!";
        s1.replace(0,5,"fuck");
        cout<<s1<<endl; //输出fuck World!
        //指定替换 例如将World! 中的orl 替换成ORL
        n = s1.find("orl");
        s1.replace(n,3,"ORL");
        cout<<s1<<endl; //输出fuck WORLd!
    
        //11、删除子串
        s1.erase(n,3); //n表示位置 3表示删除三个字符
        cout<<s1<<endl; //输出fuck Wd!
    
        //12、添加子串
        s1.insert(5,"aaa");//5表示位置 "aaa"表示增加的字符串
        cout<<s1<<endl; //输出fuck aaaWd!
    
        //13、STL 操作string
        string s("afgc1bed3");
        string::iterator p = find(s.begin(), s.end(), 'c');
        if (p!= s.end())
            cout << p - s.begin() << endl;  //输出3 相当于c的指针-头指针
        sort(s.begin(), s.end()); //字符串排序
        cout << s << endl;  //输出 13abcdefg
        //其余STL操作就不演示了,基本上都是支持的
        
        return 0;
    }
    
    
    
  • 相关阅读:
    Go基础---->go的基础学习(三)
    java基础---->Java关于复制的使用(一)
    java基础---->Reference的使用(一)
    java基础---->Java中枚举的使用(一)
    Go基础---->go的基础学习(一)
    java基础---->java中变参函数的使用
    java框架---->RxJava的使用(一)
    android基础---->AccessibilityService的简单使用(一)
    spring基础---->spring自定义标签(一)
    十分钟让你明白Objective-C的语法(和Java、C++的对比)
  • 原文地址:https://www.cnblogs.com/xwxz/p/14719027.html
Copyright © 2011-2022 走看看