【C++】STL——string类对象的修改操作、string类对象修改函数的介绍和使用、push_back、append、operator+

1.string类对象的修改操作

  在C++中,std::string类提供了多种方法来对字符串进行修改操作。以下是一些常用的字符串修改操作:string类详细介绍

在这里插入图片描述

在这里插入图片描述

(1)push_back(尾插单个字符)

  常用的几个:

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

  在C++中,push_back()是用于向字符串的末尾添加字符的函数。它仅适用于std::string类的对象。

  push_back()函数在向字符串中添加一个字符时,会将该字符追加到字符串的结尾处。你可以使用字符常量或者变量作为参数来调用push_back()函数,以向字符串末尾添加字符。

  下面是一个使用push_back()函数的示例:

#include <iostream>
#include <string>

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

    str.push_back(' ');
    str.push_back('W');
    str.push_back('o');
    str.push_back('r');
    str.push_back('l');
    str.push_back('d');

    std::cout << str << std::endl;

    return 0;
}

//Hello World

(2)append(尾插字符串)

  在C++中,append()函数用于将字符串或字符序列追加到现有字符串的末尾。下面是一个使用append()函数的示例:

   append()函数可以接受两种参数:字符串和字符序列。 你可以直接提供字符串常量或使用字符指针来追加字符序列。它将指定的字符串或字符序列连接到原始字符串的末尾,修改原始字符串并返回新的字符串。

#include <iostream>
#include <string>

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

    // 追加字符串
    str.append(" World");
    std::cout << str << std::endl;

    // 追加字符序列
    const char* seq = "! How are you?";
    str.append(seq);
    std::cout << str << std::endl;

    return 0;
}

//Hello World
//Hello World! How are you?

append()函数和push_back()函数的区别:

  append()函数用于追加字符串或字符序列,而push_back()函数用于将单个字符追加到字符串的末尾。

#include <iostream>
#include <string>

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

    // 使用 append() 函数进行字符串追加
    str1.append(" ");
    str1.append(str2);
    std::cout << str1 << std::endl;

    std::string str3 = "Hello";

    // 使用 push_back() 函数进行字符追加
    str3.push_back(' ');
    str3.push_back('W');
    std::cout << str3 << std::endl;

    return 0;
}

//Hello World
//Hello W

(3)operator+(尾插字符串str)

  在C++中,operator+函数用于将两个字符串进行连接操作,返回一个新的字符串。它是通过重载加号运算符来实现的。

  通过使用operator+函数,我们可以直接使用加号运算符将两个字符串连接在一起,形成一个新的字符串。这种方法更简洁,不需要调用额外的成员函数进行连接操作。

 下面是一个使用operator+函数的示例:

#include <iostream>
#include <string>

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

    std::string result = str1 + str2;
    std::cout << result << std::endl;

    return 0;
}

//Hello World

  operator+函数和append()函数虽有区别,但是用法基本相似了

   在string尾部追加字符时,s.push_back(c ) / s.append(1, c) / s += 'c’三种的实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。

#include <iostream>
#include <string>

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

    // 使用 operator+ 函数进行字符串连接
    std::string result = str1 + str2;
    std::cout << result << std::endl;

    std::string str3 = "Hello";

    // 使用 append() 函数进行字符串追加
    str3.append(str2);
    std::cout << str3 << std::endl;

    return 0;
}

//Hello World
//Hello World

(4)c_str(返回C格式字符串)

   在C++中,c_str()函数用于将std::string对象转换为C风格的字符串类型表示。 即以null终止的字符数组。

   c_str()函数的主要作用是在需要使用C风格字符串的函数或代码中,将std::string对象转换为C风格字符串。由于C风格字符串以null终止,所以必须使用const char*类型的变量来接收转换后的结果。

   需要注意的是,c_str()函数返回的C风格字符串是一个指向std::string对象内部字符数组的指针。因此,如果在后续修改std::string对象的过程中,可能会导致指针指向无效的内存。

   综上所述,通过使用c_str()函数,我们可以将std::string对象转换为C风格字符串,以便在需要的地方使用。

   以下是一个使用c_str()函数的示例:

#include <iostream>
#include <string>

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

    const char* cstr = str.c_str();

    std::cout << cstr << std::endl;

    return 0;
}

//Hello, World!

(5)find + npos(返回所找字串开始字符c位置)

  在C++中,find()函数用于在字符串中查找指定的子串,并返回第一个匹配的位置。npos是std::string类中的一个静态常量,表示无效的或不存在的位置。通过结合使用find()函数和npos常量,我们可以判断子串是否存在于目标字符串中。

  通过使用find()函数和npos常量,我们可以方便地判断子串是否存在于目标字符串中。find()函数返回的位置值可以与npos进行比较,如果相等则表明没有找到子串。这种方式是处理字符串搜索的常用方式。

  需要注意的是,npos的值在不同的编译环境中可能有所不同,但它始终是表示无效或不存在的位置。具体而言,npos的值通常是std::string::npos,表示最大可能的std::string大小。

  以下是一个使用find()函数和npos常量的示例:

#include <iostream>
#include <string>

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

    std::size_t found = str.find("World");

    if (found != std::string::npos) 
    {
        std::cout << "Subtring 'World' found at position: " << found << std::endl;
    } 
    else 
    {
        std::cout << "Substring not found" << std::endl;
    }

    return 0;
}

//Substring 'World' found at position: 7

(6)rfind(返回所找字符c位置)

  在C++中,rfind()函数用于在字符串中从末尾向前查找指定的子串,并返回最后一个匹配的位置。它类似于find()函数,但从反向开始搜索。

  通过使用rfind()函数,我们可以从目标字符串的末尾向前搜索子串。这可以用于查找最后一个匹配的位置,或者在处理需要从后向前进行搜索的特定问题时使用。

  需要注意的是,与find()函数类似,在不同的编译环境中,npos的值也可能有所不同,但它仍然表示无效或不存在的位置。具体而言,npos的值通常是std::string::npos,表示最大可能的std::string大小。

  以下是一个使用rfind()函数的示例:

#include <iostream>
#include <string>

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

    std::size_t found = str.rfind("Hello");

    if (found != std::string::npos) 
    {
        std::cout << "Last occurence of substring 'Hello' found at position: " << found << std::endl;
    } 
    else 
    {
        std::cout << "Substring not found" << std::endl;
    }

    return 0;
}

//Last occurence of substring 'Hello' found at position: 13

(7)substr(截取字符串返回)

  在C++中,substr()函数用于提取字符串中的子串。它接受两个参数,第一个参数是提取子串的起始位置,第二个参数是提取子串的长度。

  通过使用substr()函数,我们可以从字符串中提取需要的子串,并将其存储在一个新的字符串对象中。

  需要注意的是,substr()函数的第一个参数指定的是起始位置,而不是子串的结束位置。例如,在示例中,起始位置7是指从索引为7的字符开始提取。第二个参数指定的是子串的长度,而不是结束位置。在示例中,长度为5表示从起始位置开始,提取5个字符作为子串。

  如果只提供一个参数给substr()函数,则会从指定位置一直提取到字符串的末尾。

  综上所述,通过使用substr()函数,我们可以方便地从字符串中提取指定位置和长度的子串。

  以下是一个使用substr()函数的示例:

#include <iostream>
#include <string>

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

    std::string substr = str.substr(7, 5);

    std::cout << "Substring: " << substr << std::endl;

    return 0;
}

//Substring: World

这些就是C++中string类对象的修改和操作的简单介绍了😉
如有错误❌望指正,最后祝大家学习进步✊天天开心✨🎉

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鳄鱼麻薯球

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

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

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

打赏作者

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

抵扣说明:

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

余额充值