C++,STL 020(2024.09.28)

内容:string的插入删除操作。

(ps:今天本来想摆烂的,后来想了想,还是算了……)

#include <iostream>
#include <string>

using namespace std;

/*
注意:
1. string& insert(int pos, const string& str);  --> 插入string型字符串
   string& insert(int pos, const char* str);    --> 插入char*型字符串
   string& insert(int pos, int n, char c);      --> 从位置pos开始插入n个字符c
2. string& erase(int pos, int n = npos);  --> 从pos位置开始删除后面所有字符
   string& erase(int pos, int n);         --> 从pos位置开始删除n个字符
3.字符串首位置为0
*/

void test01()
{
    string str1 = "aaaaaa";

    str1.insert(1, "bbb"); // here,插入02
    cout << str1 << endl;

    str1.erase(4); // here,删除01
    cout << str1 << endl;

    str1.erase(1, 3); // here,删除02
    cout << str1 << endl;
}

void test02()
{
    string str2 = "aaaaaa";
    string str3 = "ccc";

    str2.insert(1, str3); // here,插入01
    cout << str2 << endl;

    str3.insert(1, 3, 'd'); // here,插入03
    cout << str3 << endl;
}

int main()
{
    test01();

    test02();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值