c++string常用函数

String 是一种常用的数据类型,它表示一个字符序列。在 c++ 中,字符串有许多可用的函数和方法。以下是一些常用的函数:

1.

std::string::size(): 返回字符串的长度。

2.

std::string::length(): 返回字符串的长度。

3.

std::string::empty(): 检查字符串是否为空。

4.

std::string::clear(): 清空字符串。

5.

std::string::insert(): 在指定位置插入一个字符或字符串。

6.

std::string::erase(): 删除指定位置的字符或子字符串。

7.

std::string::replace(): 替换指定位置的字符或子字符串。

8.

std::string::append(): 在字符串末尾添加一个字符或字符串。

9.

std::string::compare(): 比较两个字符串。

10.

std::string::substr(): 返回字符串的指定子字符串。

11.

std::string::find(): 查找指定字符或字符串在字符串中首次出现的位置。

12.

std::string::rfind(): 查找指定字符或字符串在字符串中最后一次出现的位置。

13.

std::string::find_first_of(): 查找指定字符或字符串在字符串中首次出现的位置。

14.

std::string::find_last_of(): 查找指定字符或字符串在字符串中最后一次出现的位置。

15.

std::string::at(): 访问字符串中的字符。

下面是这些函数的一些示例用法:

#include <iostream>
#include <string>

int main() {
    std::string s = "Hello, World!";

    // 字符串长度
    std::cout << "String length: " << s.length() << std::endl;

    // 在字符串末尾添加字符
    s.append("123");
    std::cout << "String after appending 123: " << s << std::endl;

    // 查找字符 'l' 在字符串中首次出现的位置
    std::string::size_type pos = s.find('l');
    std::cout << "First occurrence of 'l': " << pos << std::endl;

    // 查找子字符串 "World" 在字符串中首次出现的位置
    pos = s.find("World");
    std::cout << "First occurrence of 'World': " << pos << std::endl;

    // 替换字符 'l' 为字符 'L'
    s.replace(pos, 1, "L");
    std::cout << "String after replacing 'l' with 'L': " << s << std::endl;

    // 删除字符 'L'
    s.erase(pos, 1);
    std::cout << "String after erasing 'L': " << s << std::endl;

    return 0;
}
这些函数都可以在 std::string 类型的对象上调用,它们提供了丰富的功能,足以应对大多数字符串操作。
C++:

在 C++ 中,将数值转换为字符串可以使用 std::to_string 函数。这个函数定义在 std 命名空间中,因此需要包含 <string> 头文件。以下是如何使用 std::to_string 的示例:

#include <string>
#include <iostream>

int main() {
    // 将整数转换为字符串
    int age = 25;
    std::string age_str = std::to_string(age);
    std::cout << "Age: " << age_str << std::endl;

    // 将浮点数转换为字符串
    double pi = 3.14159;
    std::string pi_str = std::to_string(pi);
    std::cout << "Pi: " << pi_str << std::endl;

    // 将布尔值转换为字符串
    bool is_valid = true;
    std::string is_valid_str = std::to_string(is_valid);
    std::cout << "Is valid: " << is_valid_str << std::endl;

    return 0;
}

#include <string> // 包含字符串操作的头文件
#include <iostream> // 包含输入输出操作的头文件

int main() { // 主函数,程序从这里开始执行
    std::string str = "Hello, world! Hello, universe!"; // 定义并初始化字符串变量
    std::string sub = "Hello"; // 定义并初始化子字符串变量

    // 计算子字符串 "Hello" 出现的次数
    size_t count = 0; // 定义计数器变量
    size_t pos = 0; // 定义起始位置变量

    while ((pos = str.find(sub, pos)) != std::string::npos) { // 使用 while 循环查找子字符串
        ++count; // 如果找到子字符串,增加计数器
        pos += sub.length(); // 更新下一个查找的起始位置
    }

    std::cout << "Count of sub-string: " << count << std::endl; // 输出子字符串出现的次数

    return 0; // 退出主函数,结束程序
}

while ((pos = str.find(sub, pos)) != std::string::npos) 详细讲一下这个

这一行代码是 while 循环的条件。它使用 std::string::find 方法在 str 中查找子字符串 sub,并从指定的位置 pos 开始。循环将重复执行,直到找不到子字符串为止。

std::string::find 方法返回一个 size_t 类型的值,表示子字符串在 str 中的位置。如果找不到子字符串,它将返回 std::string::npos,这是一个特殊的值,表示不存在的位置。

赋值运算符 = 的右侧计算 str.find(sub, pos) 的结果,并将结果赋给 pos。这意味着,无论 std::string::find 返回什么值,都将被赋给 pos。这将使 pos 更新为下一个查找的起始位置。

当 pos 被更新为 std::string::npos 时,条件 (pos = str.find(sub, pos)) != std::string::npos 不再成立,循环将结束。

在循环的每次迭代中,都会检查条件 (pos = str.find(sub, pos)) != std::string::npos。如果条件为真,则增加计数器 count,并将 pos 更新为下一个查找的起始位置。否则,循环将结束。

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小小宇宙中微子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值