string容器相关操作

#include <iostream>
#include <string>

using namespace std;

/**
 *@NWNU ZIYIF
 */

//string本身是一个类
//string类中封装了很多成员方法,ex:find,copy,delete,replace,insert
//char*是个指针,string是一个类,类内部封装了char*,管理这个字符串,是一个char*型容器

void try1();//string的构造
void try2();//string的赋值和拼接操作(赋值一般用等号的比较多)
void try3();//string的查找
void try4();//string的替换
void try5();//string的比较(通过ASCII码比较)=返回0,>返回1,<返回-1,string中已经重载了==,所以直接==也可以判断
void try6();//string的存取
void try7();//string的插入与删除
void try8();//string的子串(从字符串中获取想要的子串)

int main()
{
    //try1();
    //try2();
    //try3();
    //try4();
    //try5();
    //try6();
    //try7();
    //try8();
    return 0;
}

//string的构造函数
void try1(){
    string s1;//默认构造

    const char* str = "hello world";
    string s2(str);
    cout <<"s2=" <<s2<<endl;

    string s3 = (s2);//可以理解为把s2复制给s3
    cout <<"s3=" <<s3<<endl;

    string s4(10,'a');//构造有10个a的字符串s4,注意没有等号
    cout << "s4="<<s4<<endl;
}

//string的赋值和拼接操作(一般用等号的比较多)
/*string& assign(const char *s,int n);//把字符串s的前n个字符赋给当前的字符串
string& append(const char *s,int n);//把字符串s的前n个字符连接到当前的字符串
string& append(const string &s,int pos,int n);//字符串从pos开始的n个字符连接到字符串结尾
*/
void try2(){
    string str1="hello";
    str1+=" world";
    str1+='!';
    cout<<"str1="<<str1<<endl;

    string str2=str1;
    str1+=str2;
    cout<<"str1="<<str1<<endl;
    cout<<"str2="<<str2<<endl;

    string str3;//不能直接在定义时初始化
    str3='a';
    str3.append("hello",5);
    cout<<"str3="<<str3<<endl;

    string str4;
    str4.assign("hello world");
    str4.append(str3);
    cout<<"str4="<<str4<<endl;

    string str5;
    str5.assign("hello world",5);
    cout<<"str5="<<str5<<endl;
    str5.append("hello",0,5);//字符串中的pos从0开始的后五个字符
    cout<<"str5="<<str5<<endl;

    string str6;
    str6.assign(str5);
    cout<<"str6="<<str6<<endl;

    string str7;
    str7.assign(10,'a');
    cout<<"str7="<<str7<<endl;

}

//字符串查找
void try3(){
    string str1="abcdefgde";//字符串从0开始编号
    int pos=str1.find("de");//有了返回第一次出现的pos(从0开始的编号),没有返回-1
    if(pos==-1){
        cout<<"未找到"<<endl;
    }
    else{
        cout<<"pos="<<pos<<endl;
    }
    //rfind是从右往左,find是从左往右
    pos=str1.rfind("de");//有了返回最后一次出现的pos(从0开始的编号),没有返回-1
    cout<<"rpos="<<pos<<endl;
}

//字符串替换
void try4(){
    string str1="abcdefg";//字符串从0开始编号
    str1.replace(0,3,"de");//替换从1开始3个字符替换为字符串de
    cout<<"str1="<<str1<<endl;

}

//字符串的比较
void try5(){
    string s1,s2;
    s1="hello";
    s2="zello";
    int temp=s1.compare(s2);//比较过程中遇到第一个不相等的字符就会停止比较,所以后面的字符完全没有影响
    /*if(!temp){
        cout<<"s1=s2"<<endl;
    }
    else if(temp>0){
        cout<<"s1>s2"<<endl;
    }
    else{
        cout<<"s1<s2"<<endl;
    }*/
    if(s1==s2){//string中已经重载了==,所以直接==也可以判断
        cout<<"xiangdeng"<<endl;
    }
    else if(s1>s2){
        cout<<">"<<endl;
    }
    else{
        cout<<"<"<<endl;
    }
}

//字符串的存取
void try6(){
    string s="hello";
    cout<<"s="<<s<<endl;
    //单个字符的访问:1.通过[]
    for(int i=0;i<s.size();i++){
        cout<<s[i]<<" ";
    }
    cout<<endl;
    //2.通过at方式
    for(int i=0;i<s.size();i++){
        cout<<s.at(i)<<" ";
    }
    cout<<endl;
    //单个字符的修改
    s[0]='z';
    cout<<s<<endl;
    s.at(0)='h';
    cout<<s<<endl;
}

//string的插入和删除
void try7(){
    string s="hello";
    //insert
    s.insert(4,"world");//如果从0开始数的话,就是在第4个字符前插入world
    cout<<s<<endl;//hellworldo
    //delete
    s.erase(4,5);//从第4个字符开始(包含第4个字符)以后的5个字符
    cout<<s<<endl;
}

//string的子串
void try8(){
    string s="abcdef";
    string subs=s.substr(1,3);//从0开始从第1开始数,数3个
    cout<<subs<<endl;
    string email="ziyifan.824@foxmail.com";
    int pos=email.find('@');
    cout<<"pos="<<pos<<endl;
    string username=email.substr(0,pos);
    cout<<"username="<<username<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值