C++ STL 系列(一):string 容器详解与示例

文章目录

仓库链接
本仓库提供STL相关每一篇文章的完整可运行示例代码。

bash 复制代码
git clone https://github.com/MaikieMaiky/cpp_STL.git

string基本概念

本质:

  • string是C++风格的字符串,本质是类

string和char*区别:

  • char*是一个指针
  • string是一个类,内部封装了char*,是一个char*型的容器

特点:

  • string类内部封装了很多成员方法

  • 例如:find copy delete replace insert

  • string自动管理char*的内存分配,并且无需担心复制越界和取值越界,由类内部负责


string构造函数

原型:

  • string() - 创建空字符串

  • string(const char* s) - 使用c风格字符串初始化

  • string(const string &str) - 使用一个string对象初始化另一个string对象,左值右值皆可

  • string(int n, char c) - 使用n个字符c初始化

  • 示例

    cpp 复制代码
    // string的构造函数
    /**
     * string(); // 创建一个空的字符串 例如: string str;
     * string(const char* s); // 使用字符串s初始化
     * string(const string& str); // 使用一个string对象初始化另一个string对象
     * string(int n, char c); // 使用n个字符c初始化
     * 
     */
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void test0()
    {
        // 1. 默认构造
        string s1; 
    
        // 2. 使用字符串s初始化
        const char* str = "hello world";
        string s2(str); // 使用字符串s初始化
    
        // 3. 使用一个string对象初始化另一个string对象
        string s3(s2);
    
        // 4. 使用n个字符c初始化
        string s4(10, 'a');
    
        cout << "s2 = " << s2 << endl;
        cout << "s3 = " << s3 << endl;
        cout << "s4 = " << s4 << endl;
    }
    
    int main()
    {
        test0();
        return 0;
    }

构造函数按需灵活使用即可


string赋值操作

原型 =重载/成员函数

  • string& operator=(const char* s); // 使用字符串s赋值

  • string& operator=(const string& s); // 使用一个string对象赋值

  • string& operator=(char c); // 使用字符c赋值

  • assign(const char* s); // 使用字符串s赋值

  • assign(const char* s, int n); // 使用字符串s的前n个字符赋值

  • assign(const string& s); // 使用一个string对象赋值

  • assign(int n, char c); // 使用n个字符c赋值

  • 示例

    cpp 复制代码
    // string的赋值函数
    /**
     * string& operator=(const char* s);    // 使用字符串s赋值
     * string& operator=(const string& s);  // 使用一个string对象赋值
     * string& operator=(char c);           // 使用字符c赋值
     * assign(const char* s);               // 使用字符串s赋值
     * assign(const char* s, int n);        // 使用字符串s的前n个字符赋值
     * assign(const string& s);             // 使用一个string对象赋值
     * assign(int n, char c);               // 使用n个字符c赋值
     *
     */
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void test0()
    {
        string s1;
        s1 = "hello world";
    
        string s2;
        s2 = s1;
    
        string s3;
        s3 = 'a';
    
        string s4;
        s4.assign("hello world");
    
        string s5;
        s5.assign("hello world", 5);
        
        string s6;
        s6.assign(s1);
    
        string s7;
        s7.assign(10, 'a');
    
        cout << "s1 = " << s1 << endl;
        cout << "s2 = " << s2 << endl;
        cout << "s3 = " << s3 << endl;
        cout << "s4 = " << s4 << endl;
        cout << "s5 = " << s5 << endl;
        cout << "s6 = " << s6 << endl;
        cout << "s7 = " << s7 << endl;
    }
    
    int main()
    {
        test0();
        return 0;
    }

string赋值方式很多,operator= 比较实用


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); // 把字符串s连接到当前字符串结尾

  • string& append(const string& s, int pos, int n);// 把字符串s从pos开始的n个字符连接到当前字符串结尾

  • string& append(int n, char c); // 在当前字符串结尾添加n个字符c

  • 示例

    cpp 复制代码
    // 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);                 // 把字符串s连接到当前字符串结尾
     * string& append(const string& s, int pos, int n); // 把字符串s从pos开始的n个字符连接到当前字符串结尾
     * string& append(int n, char c);                   // 在当前字符串结尾添加n个字符c
     * 
     */
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void test0()
    {
        string s1 = "hello";
        s1 += " world";
    
        string s2 = "hello";
        s2 += 'b';
    
        string s3 = "hello";
        s3 += s1;
        
        string s4 = "hello";
        s4.append(" world");
        
        string s5 = "hello";
        s5.append(" world", 3);
    
        string s6 = "hello";
        s6.append(s1);
    
        string s7 = "hello";
        s7.append(s1, 5, 6);
    
        string s8 = "hello";
        s8.append(5, 'b');
    
        cout << "s1 = " << s1 << endl;
        cout << "s2 = " << s2 << endl;
        cout << "s3 = " << s3 << endl;
        cout << "s4 = " << s4 << endl;
        cout << "s5 = " << s5 << endl;
        cout << "s6 = " << s6 << endl;
        cout << "s7 = " << s7 << endl;
        cout << "s8 = " << s8 << endl;
    }
    
    int main()
    {
        test0();
        return 0;
    }

string查找和替换

功能:

  • 查找:查找指定字符串是否存在
  • 替换:在指定的位置替换字符串

原型:

  • int find(const string& str, int pos = 0) const; // 查找str第一次出现位置,从pos开始查找

  • int find(const char* s, int pos = 0) const; // 查找s第一次出现位置,从pos开始查找

  • int find(const char* s, int pos, int n) const; // 查找s前n个字符第一次出现位置,从pos开始查找

  • int find(const char c, int pos = 0) const; // 查找c第一次出现位置,从pos开始查找

  • int rfind(const string& str, int pos = npos) const; // 查找str最后一次出现位置,从pos开始查找

  • int rfind(const char* s, int pos = npos) const; // 查找s最后一次出现位置,从pos开始查找

  • int rfind(const char* s, int pos, int n) const; // 查找s前n个字符最后一次出现位置,从pos开始查找

  • int rfind(const char c, int pos = npos) const; // 查找c最后一次出现位置,从pos开始查找

  • string& replace(int pos, int n, const string& str); // 替换从pos开始的n个字符为str

  • string& replace(int pos, int n, const char* s); // 替换从pos开始的n个字符为s

  • 示例

    cpp 复制代码
    // string的查找和替换
    /**
     * int find(const string& str, int pos = 0) const;      // 查找str第一次出现位置,从pos开始查找
     * int find(const char* s, int pos = 0) const;          // 查找s第一次出现位置,从pos开始查找
     * int find(const char* s, int pos, int n) const;       // 查找s前n个字符第一次出现位置,从pos开始查找
     * int find(const char c, int pos = 0) const;           // 查找c第一次出现位置,从pos开始查找
     * int rfind(const string& str, int pos = npos) const;  // 查找str最后一次出现位置,从pos开始查找
     * int rfind(const char* s, int pos = npos) const;      // 查找s最后一次出现位置,从pos开始查找
     * int rfind(const char* s, int pos, int n) const;      // 查找s前n个字符最后一次出现位置,从pos开始查找
     * int rfind(const char c, int pos = npos) const;       // 查找c最后一次出现位置,从pos开始查找
     * string& replace(int pos, int n, const string& str);  // 替换从pos开始的n个字符为str
     * string& replace(int pos, int n, const char* s);      // 替换从pos开始的n个字符为s
     * 
     */
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    // 1. find
    void test0()
    {
        string str = "abcdefgde";
        int pos = str.find("de");
    
        cout << "pos = " << pos << endl;
    
        pos = str.rfind("de");
        cout << "pos = " << pos << endl;
    
    }
    
    // 2. replace
    void test1()
    {
        string str = "abcdefgde";
        // 把1-3位置的字符替换为1111这个字符串
        str.replace(1, 3, "1111");
        cout << "str = " << str << endl;
    
        
    }
    
    int main()
    {
        test1();
        return 0;
    }

find与rfind区别:

  • find从左往右找
  • rfind从右往左找

find not found return std::string::npos which is often -1


string比较

比较方式:

  • 按照字符的ASCII码逐个对比
  • = 返回0
  • 返回1

  • < 返回 -1

原型:

  • int compare(const string& s) const // 与字符串s比较

  • int compare(const char* s) const // 与字符串s比较

  • 示例(仅包含必要部分)

    cpp 复制代码
    void test0()
    {
        string s1 = "xello";
        string s2 = "hello";
        if (s1.compare(s2) == 0)
        {
            cout << "s1 == s2" << endl;
        }
        else if (s1.compare(s2) > 0)
        {
            cout << "s1 > s2" << endl;
        }
        else
        {
            cout << "s1 < s2" << endl;
        }
    }

字符串一般用于判断相等,谁大谁小没有意义


string字符存取

两种方式: / at

原型:

  • char& operator[](int n); // 通过\[\]方式取字符

  • char& at(int n); // 通过at方法获取字符

  • 示例(仅包含必要部分)

    cpp 复制代码
    void test0()
    {
        // 1. []
        string str = "hello";
        for (int i = 0; i < str.size(); i++)
        {
            cout << str[i] << " ";
        }
        cout << endl;
        
        // 2. at
        for (int i = 0; i < str.size(); i++)
        {
            cout << str.at(i) << " ";
        }
        cout << endl;
    
        // 3. 修改
        str[0] = 'b';
        str.at(1) = 'b';
        cout << "str = " << str << endl;
    }

string 插入和删除

插入和删除字符串

原型:

  • string& insert(int pos, const char* s); // 在pos位置插入字符串s

  • string& insert(int pos, const string& str); // 在pos位置插入字符串str

  • string& insert(int pos, int n, char c); // 在pos位置插入n个字符c

  • string& erase(int pos = 0, int n = npos); // 删除从pos开始的n个字符

  • 示例(仅包含必要部分)

    cpp 复制代码
    void test0()
    {
        string str = "hello";
    
        // 1. 插入
        str.insert(1, "111");
        cout << "str = " << str << endl;
        
        // 2. 删除
        str.erase(1, 3);
        cout << "str = " << str << endl;
    }

插入和删除的起始下标都是从0开始的


string子串获取

从string中截取需要的子串,比如配合find截取邮箱中@之前的名字

灵活使用取字串,可以方便的分别取出string的有效信息

原型:

  • string substr(int pos = 0, int n = npos) const; // 返回从pos开始的n个字符组成的字符串

  • 示例(仅包含必要部分)

    cpp 复制代码
    void test0()
    {
        string str = "LeiJun@gmail.com";
    
        // 取出用户名
        int pos = str.find('@');
        string username = str.substr(0, pos);
        cout << "username: " << username << endl;
    }
相关推荐
小小测试开发36 分钟前
安装 Python 3.10+
开发语言·人工智能·python
好评1242 小时前
【C++】智能指针全解
c++·智能指针
AAA大运重卡何师傅(专跑国道)2 小时前
【无标题】
开发语言·c#
是阿建吖!2 小时前
【Linux】信号
android·linux·c语言·c++
城北徐宫2 小时前
Linux信号深度解剖:5种产生、3张表、4次切换
linux·c++·学习
liulilittle2 小时前
论 Linux 内核态全局稳态带宽的卡尔曼估计与工程实现
linux·服务器·网络·c++·计算机网络·tcp·通信
XBodhi.3 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
LSssT.3 小时前
【01】Python 机器学习
开发语言·python
l1t4 小时前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程39-40
开发语言·python
曾阿伦4 小时前
Python 搭建简易HTTP服务
开发语言·python·http