• 【STL常用容器】:string 容器


    前言

    时不可以苟遇,道不可以虚行。


    一、string容器的基本概念

    • 本质: string 是 C++ 风格的字符串,而 string 本质上是一个类。

    stringchar* 的区别:

    • char * 是一个指针
    • string 是一个类,类内部封装了 char * ,管理这个字符串,是一个 char* 型的容器。

    特点:

    • string 类内部封装了很多成员方法
    • 例如:查找 find、拷贝 copy、删除 delete、替换 replace、插入 insert
    • string 管理 char* 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责。

    二、字符串的创建构造

    • 字符串类具有许多可用于创建字符串的构造函数。

    常用的四种方式:

    1. string(); 创建一个空字符串,例如:string str;
    	string s1;      //默认构造
    
    • 1
    1. string(const char* s); :使用字符串 s 初始化
    	const char * str = "OilPicture";
        string s2(str);
    
    • 1
    • 2
    1. string(const string& str); :使用一个 string 对象初始化另一个 string 对象,类似于拷贝构造
    	const char * str = "OilPicture";
        string s2(str);
    	string s3(s2);
    
    • 1
    • 2
    • 3
    1. string(int n, char c); :使用 n 个字符 c 初始化字符串,最后输出结果为:打印 由10个字符 w 组成的字符串。
    	string s4(10,'w');
    	cout << "s4 = " << s4 << endl;
    
    • 1
    • 2

    三、string 赋值操作

    赋值的函数原型:操作符 operator= 和 成员函数 assign

    • 赋值操作方法:
      在这里插入图片描述
    • 构造代码:
      在这里插入图片描述
    • 结果输出:
      在这里插入图片描述

    四、string 字符串拼接

    • 为了实现在字符串末尾拼接字符串。

    函数原型:

    • string& operator+=(const char* str);:重载 += 操作符
    • string& operator+=(const char c); :重载 += 操作符
    • string& operator+=(const string& str); :重载 += 操作符
    • string& append(const char *s); :把字符串 s 连接到当前字符串结尾
    • string& append(const char *s, int n); :把字符串 s 的前 n 个字符连接到当前字符串结尾
    • string& append(const string &s); :同 operator+=(const string& str);
    • string& append(const string &s, int pos, int n); :将字符串 s 中从 pos 开始的 n 个字符连接到字符串结尾

    • 定义一个字符串,直接用 += 加上一串字符:
    	string str1 = "我";
        str1 += "爱玩游戏";
        cout << "str1 = " << str1 << endl;
    
        str1 += ":";
        cout << "str1 = " << str1 << endl;
    
        string str2 = "LOL DNF";
        str1 += str2;
        cout << "str1 = " << str1 << endl;
    
    
        string str3 = "I";
        str3.append(" Love");
        cout << "str3 = " << str3 << endl;
    
        str3.append(" gameStory",4);
        cout << "str3 = " << str3 << endl;
    
        str3.append(str2);
        cout << "str3 = " << str3 << endl;
    
        str3.append(str2,0,3);      //只截取到:LOL
        cout << "str3 = " << str3 << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    五、string 查找和替换

    • 查找索引开始的起点从 0 开始,索引失败即没有查找字符的情况下,会返回 -1

    查找函数:

    • find :从左往右查找
    • rfind :从右往左查找
    	string s1 = "OilPicture";   //从0开始索引,没有情况下,会返回-1
        //find
        int pos1 = s1.find("i");		//pos1=1
        int pos2 = s1.rfind("i");		//pos2=4
    
    • 1
    • 2
    • 3
    • 4

    替换函数:

    • replace(pos, len, string s); :索引位置从 0 开始。
      在这里插入图片描述
    	string str1 = "abcdefg";
    
        //从1号位置起 3个字符,替换为 "1111"
        str1.replace(1,3,"1111");
    
    • 1
    • 2
    • 3
    • 4
    • 注: replace 在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串。

    六、string字符串比较

    比较方式:字符串比较是按字符的 ASCII 值进行对比

    • =,返回:0
    • >,返回:1
    • <,返回:-1

    函数原型:

    • int compare(const string &s) const; :与字符串 s 比较
    • int compare(const char *s) const; :与字符串 s 比较
    	string str1 = "Oil";
        string str2 = "Oil";
    
        if(str1.compare(str2) == 0) {
            cout << "str1 等于 str2" << endl;
        }else if(str1.compare(str2) > 0) {
            cout << "str1 大于 str2" << endl;
        }else {
            cout << "str1 小于 str2" << endl;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    七、string 字符的存取

    string 中单个字符存取方式有两种:

    • char& operator[](int n); :通过 [] 方式取字符
    • char& at(int n); :通过 at 方法获取字符

    • 随便定义一个字符串:
    	string str = "Hello";
    
    • 1
    • 1、通过 [] 访问单个字符。
    	for (int i = 0; i < str.size(); ++i) {
            cout << str[i] << " ";
        }
        cout << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 2、通过 at 方式访问单个字符。
    	for (int i = 0; i < str.size(); ++i) {
            cout << str.at(i) << " ";
        }
        cout << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 修改单个字符:以下两种方法都可。
    	str[0] = 'W';
    
    	str.at(1) = 'W';
    
    • 1
    • 2
    • 3

    八、string的插入和删除

    函数原型:(开始索引均是从 0 开始)

    • string& insert(int pos, const char* s); :插入字符串
    • string& insert(int pos, const string& str); :插入字符串
    • string insert(int pos, int n, char c); :在指定位置插入 n 个字符 c
    • string& erase(int pos, int n = npos); :删除从 pos 开始的 n 个字符
    	string str = "Hello";
    
        //插入
        str.insert(1,"wz");
        //Hello --> Hwzello
        cout << "str = " << str << endl;
    
        //删除:从第几个位置删除几个元素
        str.erase(1,2);
        cout << "str = " << str << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 注: 插入和删除的起始下标都是从 0 开始。

    九、string 子串

    • 作用: 从字符串中获取想要的子串。
    • 函数原型: string substr(int pos=0, int n=npos) const; :返回由 pos 开始组成的字符串

    	string str = "abcdef";
    
        string subStr = str.substr(1,3);
    
        //subStr --> bcd
        cout << "subStr = " << subStr << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 将原字符串的下标由 13 的子串取出(索引下标从 0 开始)

    例:取出邮箱中的用户名

    	string email = "oilpicture@sina.com";
    
        //从邮箱地址中获取用户名信息
        int pos = email.find("@");
        cout << "@符的位置为:" << pos << endl;
        string usrName = email.substr(0,pos);
        cout << usrName << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • @ 符作为分隔符,然后取出由下标 0@ 符位置下标的子字符串,即为邮箱的用户名。
  • 相关阅读:
    C++_默认值拷贝构造函数_构造函数顺序
    Flink 提交到 yarn 时 修改 yarn 的job 名称
    《Python 快速入门》C站最全Python标准库总结
    MATLAB实战应用案例精讲(二)-【图像处理】图像分类(附MATLAB代码实现)
    Codeforces Global Round 23 E CF1746E Joking (Hard Version)
    新版IDEA没有办法选择Java8版本解决方法
    基于单片机的指纹门禁设计
    探店通源码。短视频矩阵源码,look here
    PyTorch学习笔记(二)
    2558. 从数量最多的堆取走礼物
  • 原文地址:https://blog.csdn.net/WandZ123/article/details/128098086