10 string容器 - 字符串插入和删除

功能描述:对string字符串进行插入和删除字符操作

函数原型:

string& insert(int pos, const char* s); //插入字符串

string& insert(int pos, const string& str); //插入字符串

string& insert(int pos, int n, char c); //在指定位置插入n个字符c

string& erase(int pos, int n = npos); //删除从Pos开始的n个字符

cpp 复制代码
#include<iostream>
using namespace std;

// 字符串 插入和删除
void test01() {
	string str = "hello";

	// 插入
	str.insert(1, "111");

	cout << "str = " << str << endl; // h111ello

	// 删除
	str.erase(1, 3);

	cout << "str = " << str << endl; // hello
}

int main() {
	test01();

	system("pause");
	return 0;
}

总结:插入和删除的起始下标都是从0开始

相关推荐
eggwyw4 分钟前
C++设计模式之解释器模式
开发语言·c++
mengge.cloud2 小时前
Python基础保姆级!从入门到入土
开发语言·python·云原生·pygame
在爪洼山玩泥巴3 小时前
fastjson2 JSONPath 取不存在路径,为什么会抛 “not support”?
java·开发语言·jsonpath·fastjson
星落梦生13 小时前
SpringAOP基础笔记(7)
java·开发语言
我不会插花弄玉13 小时前
13.多路转接epoll【由浅入深-网络】
网络·c++·epoll
曹牧15 小时前
Java:“刷新雪崩”
java·开发语言
郝学胜-神的一滴15 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
西西弗Sisyphus16 小时前
C++ explicit 关键字工程实践
开发语言·c++
郝学胜-神的一滴17 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
feixing_fx18 小时前
原始类型与引用类型:赋值时到底复制了什么
开发语言·前端·javascript·前端框架