JHHK

欢迎来到我的个人网站
行者常至 为者常成

18、string

目录

C字符串

void cstring_Test(){
    //实现方式一:
    char str1[6] = {'h','e','l','l','o','\0'};
    cout<<"str1 = "<<str1<<endl;
    
    //实现方式二:
    char str2[6] = "hello";
    cout<<"str2 = "<<str2<<endl;
    
    //实现方式三:
    char * str3 = "hello";
    cout<<"str3 = "<<str3<<endl;
    
    /**
     str1 = hello
     str2 = hello
     str3 = hello
     */
}
void cstring_Test2(){
    char str1[11] = "hello";
    char str2[11] = "world";
    char str3[11];
    int len;
    
    //复制 str1 到 str3
    strcpy(str3, str1);
    cout << "strcpy( str3, str1) : " << str3 << endl;
    
    //连接 str1 和 str2
    strcat(str1, str2);
    cout<<"strcat(str1, str2) : "<<str1<<endl;
    
    //str1的长度
    len = (int)strlen(str1);
    cout<<"strlen(str1) : "<<len<<endl;
    
    //str1 和 str2 的比较
    int result = strcmp(str1, str2);
    cout<<"strcmp(str1, str2) : "<<result<<endl;
    
    //返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置
    char * chr = strchr(str1, 'e');
    cout<<"strchr(str1, 'e') : "<<chr<<endl;
    
    //返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。
    char * str = strstr(str1, str2);
    cout<<"strstr(str1, str2) : "<<str<<endl;
    
    
    /**
     strcpy( str3, str1) : hello
     strcat(str1, str2) : helloworld
     strlen(str1) : 10
     strcmp(str1, str2) : -15
     strchr(str1, 'e') : elloworld
     strstr(str1, str2) : world
     */
}

string

一、初始化方法

void constructor_test(){
    string s1 = string();
    cout<<"s1 : "<<s1<<endl;// s1 = ""

    string s2("Hello");
    cout<<"s2 : "<<s2<<endl;// s2 = "Hello"


    string s3(4, 'K');
    cout<<"s3 : "<<s3<<endl;// s3 = "KKKK"


    string s4("12345", 1, 3);
    cout<<"s4 : "<<s4<<endl;//s4 = "234",即 "12345" 的从下标 1 开始,长度为 3 的子

    //c字符串转string
    string s5 = "hello";
    cout<<"s5 : "<<s5<<endl;//s5 = "hello"
    
    
    //string 类没有接收一个整型参数或一个字符型参数的构造函数。下面的两种写法是错误的:
    //string s1('K');
    //string s2(123);

}

二、互转

void swich_test(){
    
    //string 转  c_string
    string str = "hello world";
    const char * cstr = str.c_str();
    cout<<"cstr : "<<cstr<<endl;//"hello world"

    // 数字转字符串
    int i = 123;
    string str2 = to_string(i);
    cout<<"str2 : "<<str2<<endl;//"123"

    //字符串转数字
    string str3 = "123";
    int i2 = stoi(str3);
    cout<<"i2 : "<<i2<<endl;//123
    
    string str4 = "4.5";
    float f2 = stof(str4);
    cout<<"f2 : "<<f2<<endl;
}

三、复制、连接、比较、长度

void copy_append_cmp_len_test(){
    cout<<"LCPString_TestFunc"<<endl;

    string str1 = "hello";
    string str2 = "world";
    string str3;

    // 复制 str1 到 str3
    str3 = str1;
    cout<<"str3 : "<<str3<<endl;


    //连接 str1 和 str2
    str3 = str1 + str2;
    cout<<"str3 : "<<str3<<endl;

    str3 = str1.append(str2);
    cout<<"str1"<<str1<<endl;// helloworld
    cout<<"str2"<<str2<<endl;// world
    cout<<"str3"<<str3<<endl;// helloworld


    //比较
    int result = str1.compare(str2);
    cout<<"result : "<<result<<endl;


    // 连接后,str3 的总长度
    int len = (int)str3.length();
    cout<<"len : "<<len<<endl;

    int size = (int)str3.size();
    cout<<"size : "<<size<<endl;
    
}

行者常至,为者常成!





R
Valine - A simple comment system based on Leancloud.