当前位置: 首页 > news >正文

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

目录

1.push_back() 尾插字符

2.append() 尾插字符串

3.operator+=

4.assign 覆盖

5.insert() 指定位置插入

6.erase() 删除

7.replace() 替换

8.swap() 交换

9.pop_back() 尾删


1.push_back() 尾插字符

void push_back (char c)

string s("i miss gjj");
s.push_back('!');
cout << s << endl;//i miss gjj!

2.append() 尾插字符串

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

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≥剩余字符串长度,则尾插剩余所有字符)

string s("i miss gjj");
s.append("040525", 2, 4);//i miss gjj0525

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

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个字符

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

string s("i miss gjj");
s.append(10, '!');
cout << s << endl;//i miss gjj!!!!!!!!!!

6.template <class InputIterator>

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

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)

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数据覆盖现有对象数据

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个数据覆盖现有对象数据

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

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

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个字符覆盖现有对象数据

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覆盖现有对象数据

string s("!!!");
s.assign(2, 'j');
cout << s << endl;//jj

6.template <class InputIterator>

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

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数据

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个数据

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

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个字符

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

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

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指向的范围数据

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指示范围之间的数据

string s("hello");
s.erase(2, 10);
cout << s << endl;//hestring s("hello");
s.erase(s.begin());
cout << s << endl;//ellostring 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指示范围数据

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个数据

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指向的范围数据

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指向的范围数据

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指示的范围数据

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指示的数据范围

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) 交换两个对象的数据

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() 删除最后一个字符

string s("hello gjj#");
s.pop_back();
cout << s << endl;//hello gjj

http://www.lryc.cn/news/326990.html

相关文章:

  • Elasticsearch:虚拟形象辅助和对话驱动的语音到 RAG 搜索
  • 测试开发工程师(QA)职业到底需要干些什么?part7:硬件测试工程师QA
  • Python基础:标准库 -- pprint (数据美化输出)
  • Visual Studio 小更新:改善变量的可见性
  • C++自主点餐系统
  • jconsole jvisualvm
  • python vtkUnstructuredGrid 转 vtkAlgorithmOutput_
  • IS-IS路由
  • 打造新质生产力,亚信科技2024年如何行稳致远?
  • 开源博客项目Blog .NET Core源码学习(12:App.Application项目结构分析)
  • AES加密解密算法
  • 计算机网络(05)
  • 6、ChatGLM3-6B 部署实践
  • python面试题(1~10)
  • 分类预测 | Matlab实现CNN-LSTM-Mutilhead-Attention卷积神经网络-长短期记忆网络融合多头注意力机制多特征分类预测
  • SQLServer CONCAT 函数的用法
  • python快速入门一
  • Elasticsearch 面试题及参考答案:深入解析与实战应用
  • 【ARM 嵌入式 C 入门及渐进 18 -- 字符数字转整形函数 atoi 介绍】
  • 全国超市数据可视化仪表板制作
  • react native 总结
  • 什么是自然语言处理(NLP)?自然语言处理(NLP)的概述
  • 共享旅游卡怎么使用?共享旅游卡的奥秘与魅力,解锁高效旅行的新方式
  • 使用yolov9来实现人体姿态识别估计(定位图像或视频中人体的关键部位)教程+代码
  • 「14」四个步骤,让你在直播间轻松演义你的教案……
  • 分解质因子
  • iOS18系统中,苹果可能不再使用Siri,转用Gemini
  • python笔记进阶--模块、文件及IO操作(1)
  • 单元测试框架 Junit
  • 数电票怎么查询真伪|发票识别接口|发票查验接口|PHP接口文档