小工具知识网

当前位置: 首页 > 行业知识 > C++奇迹之旅:string类对象的修改操作

行业知识

C++奇迹之旅:string类对象的修改操作

2026-11-08 08:21:26 来源:互联网转载

文章目录

  • string类的常用接口
  • string类对象的修改操作
    • push_back
    • append
    • operator+=
    • insert
    • erase
    • replace
    • find
    • c_str
    • 测试string
    • 总结

      string类的常用接口

      string网址查询:https://legacy.cplusplus.com/reference/string/string/

      string类对象的修改操作

      函数名称功能说明
      push_back在字符串后尾插字符c
      append在字符串后追加一个字符串
      operator+= (重点)在字符串后追加字符串str
      c_str(重点)返回C格式字符串
      find + npos(重点)从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
      rfind从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
      substr在str中从pos位置开始,截取n个字符,然后将其返回

      push_back

      将字符 c 追加到字符串的末尾,将其长度增加 1。

      string s1("hello world");cout     std::string str = "The quick brown fox jumps over the lazy dog.";    std::string substr = "brown";    size_t pos = str.find(substr);    if (pos != std::string::npos)     {        std::cout         std::cout     std::string str = "The quick brown fox jumps over the lazy dog.";    std::string substr = "the";    size_t pos = str.find(substr, 15);    if (pos != std::string::npos)     {        std::cout         std::cout     std::string str = "Hello, World!";    char c = 'o';    size_t pos = str.find(c);    if (pos != std::string::npos)     {        std::cout         std::cout string str;str.push_back(' '); // 在str后插入空格str.append("Hello");// 在str后追加一个字符"hello"str += 'S'; // 在str后追加一个字符'S' str += "tring"; //在str后追加一个字符串"tring"cout cout Teststring();return 0;}

上一篇:宝塔面板漏洞修复专家,专业保障网络安全,一站式解决方案!

下一篇:dw怎么网页颜色?