Day 22 C++ STL常用容器——string容器

概念

在C++中是C++标准库提供的一个字符串类模板。它被设计为更高级、更方便和更安全的字符串处理工具,相比于C风格的字符串数组,它提供了更多的功能和便捷的操作。

string类封装了一系列成员函数来操作字符串,这些成员函数包括字符串的创建、复制、拼接、查找、替换等等。它还提供了运算符重载,使得字符串的操作和使用更加直观和简洁。

当使用string时,不需要手动管理字符串的内存,它会自动处理内存的分配和释放。此外,string还提供了对字符串的边界检查,避免了缓冲区溢出等问题,提高了代码的安全性。

本质

string本质上是一个类

由于string是一个类,所以它可以使用类的特性,如构造函数、析构函数、拷贝构造函数、赋值运算符重载等。这些特性使得string类更易于使用和管理字符串数据。

综上所述,string是C++风格的字符串,本质上是一个类,在C++中广泛用于处理和操作字符串数据,提供了便利、安全和高级的字符串处理功能。

string和char 区别:

  • char * 是一个指针
  • string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
  • 内存管理:string类封装了字符串的内存管理,它会自动分配和释放内存,并且可以动态调整字符串的大小。而char*需要手动分配和释放内存,并且在操作字符串时需要确保足够的内存空间。
  • 字符串长度::string对象可以存储任意长度的字符串,并且可以使用成员函数获取字符串的长度。而char*作为一个字符数组指针,需要以空字符(‘\0’)作为字符串的结尾,使用标准库函数如strlen()来获取其长度。
  • 操作和修改:string提供了一系列成员函数来进行字符串的操作和修改,如拼接、插入、删除、查找、替换等。而char*需要使用标准库函数来进行类似的操作,如strcat()、strcpy()、strncpy()等。
  • 安全性:由于string类内部封装了字符串的操作和内存管理,对于越界访问和缓冲区溢出等问题有更好的安全性。而char*需要手动确保字符串操作的安全,容易发生内存访问错误。

特点

  • 封装了一些成员方法:std::string类提供了许多有用的成员方法来操作字符串,包括查找(find())、拷贝(copy())、删除(erase())、替换(replace())、插入(insert())等。这些成员方法使得字符串的操作更为便捷和灵活。

  • 内存管理:std::string对象会自动管理分配给它的内存空间,包括创建、扩展和释放。这意味着你无需手动管理字符串的内存,避免了复制越界和取值越界等问题。类内部会负责处理内存的分配和释放,大大提高了程序的安全性和可靠性。

string构造函数

构造函数原型

string(const char* s);
使用以空字符(‘\0’)结尾的C风格字符串s来初始化字符串对象。
例如:std::string str(“Hello”);

string(const string& str);
使用另一个std::string对象str来初始化字符串对象。
例如:std::string str1(“Hello”); std::string str2(str1);

string(int n, char c);
使用字符c重复n次初始化字符串对象。
例如:std::string str(5, ‘a’); // str的值为 “aaaaa”

string();
创建一个空的字符串对象。
例如:std::string str;

string赋值操作

赋值的函数原型

  • string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
  • string& operator=(const string &s); //把字符串s赋给当前的字符串
  • string& operator=(char c); //字符赋值给当前的字符串
  • string& assign(const char *s); //把字符串s赋给当前的字符串
  • string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
  • string& assign(const string &s); //把字符串s赋给当前字符串
  • string& assign(int n, char c); //用n个字符c赋给当前字符串

示例

std::string str1;
str1 = "Hello";  // 使用const char*类型字符串进行赋值
// 等价于 str1.operator=("Hello");

std::string str2;
std::string anotherStr = "World";
str2 = anotherStr;  // 使用另一个std::string对象进行赋值
// 等价于 str2.operator=(anotherStr);

std::string str3;
str3 = 'A';  // 使用字符进行赋值
// 等价于 str3.operator=('A');

std::string str4;
str4.assign("Hello");  // 使用const char*类型字符串进行赋值
// 等价于 str4.assign("Hello", std::strlen("Hello"));

std::string str5;
str5.assign("Hello", 3);  // 使用字符串的前3个字符进行赋值
// 等价于 str5.assign("Hel", 3);

std::string str6;
std::string anotherStr2 = "World";
str6.assign(anotherStr2);  // 使用另一个std::string对象进行赋值
// 等价于 str6.assign(anotherStr2);

std::string str7;
str7.assign(5, 'X');  // 使用5个字符'X'进行赋值
// 等价于 str7.assign("XXXXX", std::strlen("XXXXX"));

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个字符连接到字符串结尾

示例

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello";
    
    // 使用 operator+= 将 C 风格字符串拼接到 str 的末尾
    str += " World";
    std::cout << str << std::endl;  // 输出:Hello World
    
    // 使用 append 将字符串的前 n 个字符拼接到 str 的末尾
    std::string s1 = "Welcome";
    str.append(s1, 3, 4);
    std::cout << str << std::endl;  // 输出:Hello Worldcome
    
    // 使用 operator+= 将另一个 std::string 对象拼接到 str 的末尾
    std::string s2 = "!";
    str += s2;
    std::cout << str << std::endl;  // 输出:Hello Worldcome!
    
    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; //从pos位置查找s的前n个字符第一次位置

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

  • 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; //从pos查找s的前n个字符最后一次位置

  • int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置

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

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

注意:函数声明中的 const 关键字表示该成员函数不会修改类的成员变量

示例

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello World";

    // 使用 find 函数查找字符串是否存在
    int pos1 = str.find("World");
    if (pos1 != std::string::npos) {
        std::cout << "Found at position: " << pos1 << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }

    // 使用 rfind 函数从后向前查找字符串最后一次出现的位置
    int pos2 = str.rfind("l");
    if (pos2 != std::string::npos) {
        std::cout << "Last found at position: " << pos2 << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }

    // 使用 replace 函数替换指定位置的字符串
    std::string repl = "Everyone";
    str.replace(6, 5, repl);
    std::cout << "After replacement: " << str << std::endl;

    return 0;
}

string字符串比较

字符串之间的比较

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

= 返回 0

> 返回 1

< 返回 -1

函数原型

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

示例

#include <iostream>
#include <string>

int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";

    // 使用 compare 函数比较两个字符串
    int result1 = str1.compare(str2);
    if (result1 < 0) {
        std::cout << str1 << " is less than " << str2 << std::endl;
    } else if (result1 > 0) {
        std::cout << str1 << " is greater than " << str2 << std::endl;
    } else {
        std::cout << str1 << " is equal to " << str2 << std::endl;
    }

    // 使用 compare 函数比较字符串和字符数组
    const char* str3 = "Hello";
    int result2 = str1.compare(str3);
    if (result2 < 0) {
        std::cout << str1 << " is less than " << str3 << std::endl;
    } else if (result2 > 0) {
        std::cout << str1 << " is greater than " << str3 << std::endl;
    } else {
        std::cout << str1 << " is equal to " << str3 << std::endl;
    }

    return 0;
}

string字符存取

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

  • char& operator[](int n); //通过[]方式取字符
  • char& at(int n); //通过at方法获取字符
使用下标([])运算符

可以使用下标运算符来直接访问字符串中的单个字符。下标从0开始,表示第一个字符,依次递增。
例如,str[0]表示字符串str中的第一个字符。

示例

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    // 使用下标运算符访问单个字符
    char ch1 = str[0];   // 获取字符串的第一个字符
    char ch2 = str[7];   // 获取字符串的第八个字符
    
    std::cout << "ch1: " << ch1 << std::endl;
    std::cout << "ch2: " << ch2 << std::endl;
    
    return 0;
}

在上述示例中,我们创建了一个字符串对象str,将其初始化为"Hello, World!"。然后,使用下标运算符[]获取字符串中的第一个字符和第八个字符,并分别将它们存储在变量ch1ch2中。最后,我们使用std::cout输出这两个字符。

使用at()函数

另一种访问单个字符的方式是使用at()函数。at()函数与下标运算符类似,可以用来访问指定位置的字符。与下标运算符不同的是,at()函数会进行边界检查,如果访问位置超出字符串的范围,会抛出一个std::out_of_range异常。

示例

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    // 使用at()函数访问单个字符
    char ch1 = str.at(0);   // 获取字符串的第一个字符
    char ch2 = str.at(7);   // 获取字符串的第八个字符
    
    std::cout << "ch1: " << ch1 << std::endl;
    std::cout << "ch2: " << ch2 << std::endl;
    
    return 0;
}

在上述示例中,我们使用at()函数来获取字符串中的第一个字符和第八个字符,并将它们分别存储在变量ch1ch2中。然后,我们使用std::cout输出这两个字符。

无论是使用下标运算符还是at()函数,都可以用来访问和修改字符串中的单个字符。但需要注意的是,如果使用下标运算符访问超出字符串范围的位置,程序可能会发生未定义行为,因此在访问字符之前最好先检查字符串的长度。

string插入和删除

对string字符串进行插入和删除字符操作

函数原型

  • 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个字符

示例

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello";
    
    // 使用 insert 在指定位置插入字符串
    str.insert(1, "123");
    std::cout << str << std::endl;  // 输出:H123ello
    
    // 使用 erase 删除指定位置的字符
    str.erase(2, 3);
    std::cout << str << std::endl;  // 输出:H1ello
    
    return 0;
}

该示例演示了如何对字符串进行插入和删除字符操作。使用 insert
函数可以在指定位置插入字符串,这里在位置1插入字符串"123",结果为"H123ello"。使用 erase
函数可以删除指定位置的字符,这里删除位置2开始的3个字符,结果为"H1ello"。

string子串

从字符串中获取想要的子串

函数原型

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

示例

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    // 使用 substr 获取子串
    std::string sub1 = str.substr(7);  // 从位置7开始直到字符串末尾的子串
    std::cout << sub1 << std::endl;    // 输出:World!
    
    std::string sub2 = str.substr(7, 5);  // 从位置7开始的5个字符组成的子串
    std::cout << sub2 << std::endl;       // 输出:World
    
    return 0;
}

该示例演示了如何从字符串中获取子串。使用 substr 函数可以根据指定的起始位置和长度获取子串。在示例中,通过 str.substr(7) 获取从位置7开始直到字符串末尾的子串,结果为"World!“。通过 str.substr(7, 5)
获取从位置7开始长度为5的子串,结果为"World”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值