C++中string容器的修改操作

目录

[1.push_back() 尾插字符](#1.push_back() 尾插字符)

[2.append() 尾插字符串](#2.append() 尾插字符串)

3.operator+=

[4.assign 覆盖](#4.assign 覆盖)

[5.insert() 指定位置插入](#5.insert() 指定位置插入)

[6.erase() 删除](#6.erase() 删除)

[7.replace() 替换](#7.replace() 替换)

[8.swap() 交换](#8.swap() 交换)

[9.pop_back() 尾删](#9.pop_back() 尾删)


1.push_back() 尾插字符

void push_back (char c)

cpp 复制代码
string s("i miss gjj");
s.push_back('!');
cout << s << endl;//i miss gjj!

2.append() 尾插字符串

1.string& append (const string& str) 尾插字符串

cpp 复制代码
string s("i miss gjj");
s.append(",i love gjj");
cout << s << endl;//i miss gjj,i love gjj

2.string& appned (const string& str, size_t subpos, size_t sublen) 尾插字符串的第subpos位置开始的sublen字符(sublenda≥剩余字符串长度,则尾插剩余所有字符)

cpp 复制代码
string s("i miss gjj");
s.append("040525", 2, 4);//i miss gjj0525

3.string& append (char* s) 尾插指针指向的字符串

cpp 复制代码
string s("i miss gjj");
char p[] = "!!!";
s.append(p);   
cout << s << endl;//i miss gjj!!!

4.string& append (char* s, size_t n) 尾插指针指向字符串的前n个字符

cpp 复制代码
string s("i miss gjj");
char p[] = "5257";
s.append(p, 2);   
cout << s << endl;//i miss gjj52

5.string& append (char c, size_t n) 尾插n个字符c

cpp 复制代码
string s("i miss gjj");
s.append(10, '!');
cout << s << endl;//i miss gjj!!!!!!!!!!

6.template <class InputIterator>

string& append (InputIterator first, InputIterator last) 尾插迭代器指向范围的字符串

cpp 复制代码
string s1("i miss gjj");
string s2("i miss gjj");
string s(" 5257 ");
s1.append(s.begin(), s.end());
s2.append(++s.begin(), s.end() - 2);
cout << s1 << endl;//i miss gjj 5257
cout << s2 << endl;//i miss gjj525

3.operator+=

1.string& operator+= (const string& str)

2.string& operator+= (const char* s)

3.string& operator+= (char c)

cpp 复制代码
string s1(" i miss gjj !");
string s2(" gjj miss i !");
s1 += s2;
cout << s1 << endl;// i miss gjj ! gjj miss i !
s1 += "######";
cout << s1 << endl;// i miss gjj ! gjj miss i !######
s1 += '$';
cout << s1 << endl;// i miss gjj ! gjj miss i !######$

4.assign 覆盖

1.string& assign (const string& str) 用对象str数据覆盖现有对象数据

cpp 复制代码
string s1("hello world");
string s2("gjj");
s1.assign(s2);
cout << s1 << endl;//gjj

2.string& assign (const string& str, size_t subpos, size_t sublen) 用对象str的第subpos位置开始的sublen个数据覆盖现有对象数据

cpp 复制代码
string s1("hello world");
string s2("gjj");
s1.assign(s2, 1, 2);
cout << s1 << endl;//jj

3.string& assign (const char* s) 用字符串s覆盖现有对象数据

cpp 复制代码
string s("!!!");
char p[] = "gjj and i";
s.assign(p);
cout << s << endl;//gjj and i

4.string& assign (const char* s, size_t n) 用字符串s的前n个字符覆盖现有对象数据

cpp 复制代码
string s("!!!");
char p[] = "gjj and i 5257";
s.assign(p, 9);
cout << s << endl;//gjj and i

5.string& assign (size_t n, char c) 用n个字符c覆盖现有对象数据

cpp 复制代码
string s("!!!");
s.assign(2, 'j');
cout << s << endl;//jj

6.template <class InputIterator>

string& assign (InputIterator first, InputIterator last) 用迭代器指定范围覆盖现有对象数据

cpp 复制代码
string s1("!!!");
string s2("gjj");
s1.assign(s2.begin(), s2.end());
cout << s1 << endl;//gjj

5.insert() 指定位置插入

1.string& insert (size_t pos, const string& str) 在pos位置前插入对象str数据

cpp 复制代码
string s1("gjj");
string s2("love ");
s1.insert(0, s2);
cout << s1 << endl;//love gjj

2.string& insert (size_t pos, const string& str, size_t subpos, size_t sublen) 在pos位置前插入对象str从subpos位置开始的sublen个数据

cpp 复制代码
string s1("hello");
string s2(" world");
s1.insert(5, s2, 0, 6);
cout << s1 << endl;//hello world

3.string& insert (size_t pos, const char* s) 在pos位置前插入字符串s

cpp 复制代码
string s1("hello");
char p[] = " world";
s1.insert(5, p);
cout << s1 << endl;//hello world

4.string& insert (size_t pos, const char* s, size_t n) 在pos位置前插入字符串s的前n个字符

cpp 复制代码
string s1("hello ");
char p[] = "12345";
s1.insert(6, p, 3);
cout << s1 << endl;//hello 123

5.string& insert (size_t, pos, size_t n, char c) 在pos位置前插入n个字符c

void insert (iterator p, size_t n, char c) 在迭代器p指向位置前插入n个字符c

cpp 复制代码
string s1("hello ");
string s2("hello ");
s1.insert(s1.begin(), 3, '#');
s2.insert(s2.end(), 3, '#');
cout << s1 << endl;//###hello
cout << s2 << endl;//hello ###

6.iterator insert (iterator p, char c) 在迭代器p指向位置插入字符c

cpp 复制代码
string s("hello");
s.insert(s.end(), '!');
cout << s << endl;//hello!

7.template <class InputIterator>

void insert (iterator p, InputItrator first, InputItrator last) 在迭代器p指向的位置插入迭代器first和last指向的范围数据

cpp 复制代码
string s1("hello");
string s2(" world");
s1.insert(s1.end(), s2.begin(), s2.end());
cout << s1 << endl;//hello world

6.erase() 删除

1.string& erase (size_t pos = 0, size_t len = npos) 从pos位置开始删除len个字符(如果不提供参数相当于clear,删除所有数据)

2.iterator erase (iterator p) 删除迭代器p指示的位置

3.iterator erase (iterator first, iterator last) 删除迭代器first与last指示范围之间的数据

cpp 复制代码
string s("hello");
s.erase(2, 10);
cout << s << endl;//he

string s("hello");
s.erase(s.begin());
cout << s << endl;//ello

string s("hello");
s.erase(s.begin(), --s.end());
cout << s << endl;//o

7.replace() 替换

1.string& replace (size_t pos, size_t len, const string& str) 用str数据替换pos位置起的len个字符

string& replace (iterator i1, iterator i2, const string& str) 用str数据替换迭代器i1和i2指示范围数据

cpp 复制代码
string s1("hello world");
string s2("#####");
s1.replace(2, 3, s2);
cout << s1 << endl;//he##### world
s1.replace(2, 3, "$$$$$");
cout << s1 << endl;//he$$$$$## world
s1.replace(s1.begin(), s1.end(), s2);
cout << s1 << endl;//#####

2.string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen)

用str的subpos位置开始的sublen个数据替换pos位置开始的len个数据

cpp 复制代码
string s("hello world");
s.replace(6, 5, "bit", 0, 3);
cout << s << endl;//hello bit

3.string& replace (size_t pos, size_t len, const char* s) 用字符串s替代pos位置开始的len个数据

string& replace (iterator i1, iterator i2, const char* s) 用字符串s替代迭代器i1和i2指向的范围数据

cpp 复制代码
string s("hello world");
char p1[] = "gjj";
char p2[] = "world";
s.replace(6, 5, p1);
cout << s << endl;//hello gjj
s.replace(s.begin() + 6, s.end(), p2);
cout << s << endl;//hello world

4.string& replace (size_t pos, size_t len, const char*s, size_t n) 用字符串s的前n个字符替代pos位置开始的len个数据

string& replace (iterator i1, iterator i2, const char*s, size_t n) 用字符串s的前n个数据替代迭代器i1和i2指向的范围数据

cpp 复制代码
string s("hello world");
char p1[] = "gjj111";
char p2[] = "world222";
s.replace(6, 5, p1, 3);
cout << s << endl;//hello gjj
s.replace(s.begin() + 6, s.end(), p2, 5);
cout << s << endl;//hello world

5.string& replace (size_t pos, size_t len, size_t n, char c) 用n个字符c替换pos位置开始的len个数据

string& replace (iterator i1, iterator i2, size_t n, char c) 用n个字符c替换迭代器i1和i2指示的范围数据

cpp 复制代码
string s("hello world");
s.replace(6, 5, 2, 'j');
cout << s << endl;//hello jj
s.replace(s.begin() + 6, s.end(), 2, 'z');
cout << s << endl;//hello zz

6.template <class InputIterator>

string& replace (iterator i1, iterator i2, InputIterator first, InputIterator last)

用迭代器first和last指示的数据范围替换迭代器i1和i2指示的数据范围

cpp 复制代码
string s1("hello world");
string s2("gjj");
s1.replace(s1.begin() + 6, s1.end(), s2.begin(), s2.end());
cout << s1 << endl;//hello gjj

8.swap() 交换

void swap(string& str) 交换两个对象的数据

cpp 复制代码
string s1("hello world");
string s2("hello gjj");
s1.swap(s2);
cout << s1 << endl;//hello gjj
cout << s2 << endl;//hello world

9.pop_back() 尾删

void pop_back() 删除最后一个字符

cpp 复制代码
string s("hello gjj#");
s.pop_back();
cout << s << endl;//hello gjj
相关推荐
嵌入式@秋刀鱼1 小时前
《第四章-筋骨淬炼》 C++修炼生涯笔记(基础篇)数组与函数
开发语言·数据结构·c++·笔记·算法·链表·visual studio code
嵌入式@秋刀鱼1 小时前
《第五章-心法进阶》 C++修炼生涯笔记(基础篇)指针与结构体⭐⭐⭐⭐⭐
c语言·开发语言·数据结构·c++·笔记·算法·visual studio code
whoarethenext1 小时前
使用 C/C++的OpenCV 裁剪 MP4 视频
c语言·c++·opencv
愚润求学1 小时前
【递归、搜索与回溯】FloodFill算法(二)
c++·算法·leetcode
泽02021 小时前
C++之list的自我实现
开发语言·数据结构·c++·算法·list
斗转星移31 小时前
c++默认类模板参数
开发语言·c++
福理原乡大王2 小时前
Linux信号详解
linux·运维·服务器·c++·ubuntu·信号处理
扫地的小何尚2 小时前
全新NVIDIA Llama Nemotron Nano视觉语言模型在OCR基准测试中准确率夺冠
c++·人工智能·语言模型·机器人·ocr·llama·gpu
埃伊蟹黄面2 小时前
C++ —— STL容器 —— string的模拟实现
c++
Coding小公仔3 小时前
几种经典排序算法的C++实现
c++·算法·排序算法