C++之string类

#include<iostream>
#include<string>
using namespace std;

int main()
{
        char *s1="first";
        char s2[]="second";
        string s3="third";
        string s4="forth";
        char ch='@';
        //concatenate
        string s5=s3+s4;
        cout<<s5<<endl;
        string s6=s3+s2;
        cout<<s6<<endl;
        string s7=s3+ch;
        cout<<s7<<endl;
        string s8,s9,s10;
        //insert
        s8=s9="123456";
        s10="aaa";
        s8.insert(3,s10);
        cout<<s8<<endl;
        s9.insert(3,"bbb");
        cout<<s9<<endl;
        //erase
        s8.erase(3);
        s9.erase(3,1);
        cout<<s8<<endl<<s9<<endl;
        //tiquchu zichuan
        string s12="one two three two";
        string s13;
        s13=s12.substr(3,4);
        cout<<s13<<endl;
        //find
        string s14="two";
        int index=s12.find(s14,2);//ruguo 2 zhege canshu meixie,ze cong 0 kaishi chazhao
        if(index<s12.length())cout<<"found:"<<index<<endl;
        else cout<<"not found"<<endl;
        //rfind
        int index2=s12.rfind(s14,3);//zjihui chazhaodao 3 zhegeweizhi
        cout<<"index:"<<index2<<endl;
        //find_first_of
        int index3=s12.find_first_of(s14);
        cout<<"find_first_of index:"<<index3<<endl;
}

//TODO

原文链接: https://www.cnblogs.com/njit-sam/p/13225621.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++之string类

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/361404

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 下午1:48
下一篇 2023年3月2日 下午1:48

相关推荐