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

C++ string初始化、string赋值操作、string拼接操作

以下介绍了string的六种定义方式,还有很多,这个只是简单举例

#include<iostream>using namespace std;int main() {//1 无参构造string s1;cout << s1 << endl;//2 初始化构造string s2 ({'h', 'h', 'l', 'l', 'o'});cout << s2 << endl;//3 字符串初始化string s3("xiaoxiao");cout << s3 << endl;//4 字符串的前n个字符string s4("xiaoxiaoxiao", 6);cout << s4 << endl;//5 拷贝构造string s5(s4);cout << s5 << endl;// a 个 bstring s6(8, 'o');cout << s6 << endl;return 0;}

以下是string的六种赋值操作,以下是代码

#include<iostream>
using namespace std;int main() {//1 字符串常量的赋值string s1;s1 = "xiaoxiao";cout << s1 << endl;//2 字符串变量的赋值string s2;s2 = s1;cout << s2 << endl;//3 字符常量赋值string s3;s3 = 'a';cout << s3 << endl;//4 assign 接口1string s4;s4.assign("xiaoxiao");cout << s4 << endl;//5 assign 接口2string s5;s5.assign("xiaoxiaoxiao", 8);cout << s5 << endl;//6 assign 接口3string s6;s6.assign(s5);cout << s6 << endl;//7 a个bstring s7;s7.assign(8, '6');cout << s7 << endl;}

string拼接操作,代码见下

#include<iostream>using namespace std;int main() {// 1 + 运算符重载string s1 = "dada";string s2 = "xiao";s1 = s1 + s2;cout << s1 << endl;// 2 += 运算符重载string s3 = "dada";string s4 = "xiao";s4 += "daxiao";cout << s4 << endl;// 3 appendstring s5 = "abc";s5.append("def");s5.append("hijklmn", 4); // 取这个的前四个,进行对应字符串的拼接,这个4代表个数s5.append("opqrst", 2, 3);// 取从第二个开始,取三个进行拼接cout << s5 << endl;// 4 push_backstring s6 = "abb";s6.push_back('6');cout << s6 << endl;return 0;
}

http://www.xdnf.cn/news/5415.html

相关文章:

  • Celery 在分布式任务调度中的实现原理及 MQ 系统对比
  • GIF图像技术介绍
  • 隐马尔可夫模型(HMM)在彩票预测中的Java实现
  • OpenCV进阶操作:指纹验证、识别
  • 复现MAET的环境问题(自用)
  • Javascript基础语法
  • 【STM32开发】-单片机开发基础(以STM32F407为例)
  • SEO长尾关键词布局优化法则
  • 虚拟内存笔记(三)虚拟内存替换策略与机制
  • 前端项目打包部署流程j
  • 北大闰凯博士:热辐射输运问题蒙特卡罗模拟中的全局最优参考场方法
  • HTOL集成电路老化测试学习总结-20250510
  • Linux : 多线程【线程概念】
  • ssh -T git@github.com 测试失败解决方案:修改hosts文件
  • 计算机基础
  • 深入了解linux系统—— 自定义shell
  • 24、TypeScript:预言家之书——React 19 类型系统
  • MYSQL语句,索引,视图,存储过程,触发器(一)
  • 用 LVGL 打造苹果风格音量滑块:圆润无球,极简优雅
  • TCP/IP 模型每层的封装格式
  • C++ stl中的set、multiset、map、multimap的相关函数用法
  • SQL语句的优化
  • 学习和测试WebApi项目限制客户端ip访问接口(基于中间件)
  • Python httpx库终极指南
  • 端口号被占用怎么解决
  • 《Effective Python》第1章 Pythonic 思维详解——深入理解 Python 条件表达式(Conditional Expressions)
  • JAVA EE_网络原理_网络层
  • PowerShell 脚本中文乱码处理
  • 《Linux命令行大全(第2版)》PDF下载
  • TAPIP3D:持久3D几何中跟踪任意点