认认真真学习STL之string

# include<cstdio>
# include<iostream>
# include<string>


using namespace std;

int main(void)
{
    string s1("Hello");
    string s2(8,'x');
    string month = "March";
    string ob;
    cin>>ob;//string 支持流读取运算符

    string s;//string 支持getline函数
    getline(cin,s);

    string s1("cat"),s2;
    s2 = s1;//用等号赋值

    string s1("cat"),s3;
    s3.assign(s1);//用assign()成员函数进行赋值

    string s1("cartsfasf"),s3;
    s3.assign(s1,1,3);//把s1中下标为1开始的3个字符赋值给s3

    s2[5] = s1[3] = 'k';//单个字符的赋值

    string s1("good"),s2("morning");
    s1+=s2;
    cout<<s1;
    
    string s1("hello world"),s2;
    s2 = s1.substr(4,5);
    cout<<s2<<endl; 
    
    string s1("hello world");
    cout<<s1.find("ll",1)<<endl;
    cout<<s1.find("ll",2)<<endl;
    
    string s1("hello world");
    s1.replace(2,3,"haha");
    cout<<s1;//  hehaha world
    
    string s1("hellow world");
    string s2("show insert");
    s1.insert(5,s2);
    cout<<s1<<endl;
    s1.insert(2,s2,5,3);
    cout<<s1<<endl;
    
    
    

    string s1("hellow"),s2("hellow"),s3("hell");
    bool b = ( s1==s2 );
    cout<<b<<endl;
    bool b = ( s1==s3 );
    cout<<b<<endl;
    bool b = ( s1>s3 );
    cout<<b<<endl;


    //逐个访问string对象中的字符
    string s1("hellow");
    for ( int i = 0;i < s1.length();i++ )
    {
        cout<<s1.at(i)<<endl;
    }






    cout<<s1<<endl;
    cout<<s1.length()<<endl;//string 对象的成员函数用length()读取
    cout<<s2<<endl;
    cout<<month<<endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值