深入理解并掌握 C++14 中的 std::string 类

深入理解并掌握 C++14 中的 std::string 类

std::string 是 C++ 的标准库中的一个非常重要且常用的类。它为字符串处理提供了极大的便利。本文将详细介绍如何在 C++14 中使用 std::string 类,包括其所有成员函数的使用。

std::string 的基本使用

构造函数

std::string 提供了多种构造函数,我们可以使用不同的方式创建字符串:

std::string s1;                       // 创建一个空字符串
std::string s2("Hello, World!");      // 通过 C 风格字符串创建
std::string s3(s2);                   // 通过另一个 std::string 创建
std::string s4(10, 'a');              // 创建一个包含 10 个 'a' 的字符串
赋值操作

赋值操作允许我们将一个新的值赋给已经存在的 std::string 对象:

std::string s;
s = "Hello, World!";      // 通过 C 风格字符串赋值
s = s2;                   // 通过另一个 std::string 赋值
s = 'a';                  // 通过单个字符赋值
获取长度

使用 length() 或 size() 成员函数获取字符串长度:

获取长度
使用 length() 或 size() 成员函数获取字符串长度:
访问字符

使用 operator[] 或 at() 成员函数访问特定位置的字符:

std::string s("Hello, World!");
std::cout << s[0];         // 输出:'H'
std::cout << s.at(0);      // 输出:'H',与 operator[] 相同,但会做越界检查

std::string 的操作函数

插入和删除

使用 insert() 和 erase() 成员函数在字符串中插入或删除子字符串:

std::string s("Hello, World!");
s.insert(5, ", dear");     // 在位置 5 插入子字符串,s 变为 "Hello, dear, World!"
s.erase(5, 6);             // 从位置 5 删除 6 个字符,s 变为 "Hello, World!"
查找和替换

使用 find() 和 replace() 成员函数查找或替换子字符串:

std::string s("Hello, World!");
size_t pos = s.find("World");  // 在 s 中查找子字符串 "World"
if (pos != std::string::npos) {
    s.replace(pos, 5, "there");  // 将 "World" 替换为 "there"
}
比较

使用 compare() 成员函数比较两个字符串:

std::string s1("Hello, World!");
std::string s2("Hello, there!");
int cmp = s1.compare(s2);       // 比较 s1 和 s2,结果为正则 s1 大于 s2,为负则 s1 小于 s2,为 0 则两者相等
子字符串

使用 substr() 成员函数获取子字符串:

std::string s("Hello, World!");
std::string sub = s.substr(0, 5);  // 获取从位置 0 开始的 5 个字符组成的子字符串,即 "Hello"
字符串连接

使用 + 或 append() 成员函数连接两个字符串:

std::string s1("Hello");
std::string s2(", World!");
std::string s = s1 + s2;          // 使用 + 连接字符串
s1.append(s2);                    // 使用 append() 连接字符串,等价于 s1 += s2;
转换为 C 风格字符串

使用 c_str() 成员函数将 std::string 转换为 C 风格字符串:

std::string s("Hello, World!");
const char* cstr = s.c_str();    // 将 std::string 转换为 C 风格字符串
清空字符串

使用 clear() 成员函数清空字符串:

std::string s("Hello, World!");
s.clear();   // 清空字符串,s 变为 ""
检查字符串是否为空

使用 empty() 成员函数检查字符串是否为空:

std::string s;
std::cout << s.empty();    // 输出:true,因为 s 为空

std::string 的迭代器

std::string 支持迭代器,因此我们可以使用迭代器来遍历字符串中的所有字符:

std::string s("Hello, World!");
for (std::string::iterator it = s.begin(); it != s.end(); ++it) {
    std::cout << *it;    // 逐个输出字符串中的字符
}

也可以使用 C++11 引入的范围基础 for 循环:

std::string s("Hello, World!");
for (char c : s) {
    std::cout << c;     // 逐个输出字符串中的字符
}

以上就是 C++14 中 std::string 类的详细介绍和使用方法。掌握 std::string 是编写高效 C++ 程序的关键,希望这篇文章对您有所帮助。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cheungxiongwei.com

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值