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

C++字符串string类常用操作详解(一)【初始化、遍历、连接】

代码示例:

[cpp]  view plain  copy
 
  1. #include <iostream>  
  2. #include "string"  
  3.   
  4. using namespace std;  
  5.   
  6. //字符串初始化  
  7. void strInit()  
  8. {  
  9.     cout << "字符串初始化:"  <<endl;  
  10.   
  11.     string s1 = "abcdefg";  //初始化方式1  
  12.     string s2("abcdefg");   //初始化方式2  
  13.     string s3 = s2;         //通过拷贝构造函数 初始化s3  
  14.     string s4(7,'s');       //初始化7个s的字符串  
  15.   
  16.     cout << "s1 = "<< s1 << endl;  
  17.     cout << "s2 = "<< s2 << endl;  
  18.     cout << "s3 = "<< s3 << endl;  
  19.     cout << "s4 = "<< s4 << endl;  
  20. }  
  21.   
  22. //字符串遍历  
  23. void strErgo()  
  24. {  
  25.     cout << "字符串遍历:"  <<endl;  
  26.   
  27.     string s1 = "abcdefg";  //初始化字符串  
  28.       
  29.     //通过数组方式遍历  
  30.     cout << "1、通过数组方式遍历:"  <<endl;  
  31.     for (int i = 0; i < s1.length(); i++)  
  32.     {  
  33.         cout << s1[i] << " ";  
  34.     }  
  35.     cout << endl;  
  36.   
  37.     //通过迭代器遍历  
  38.     cout << "2、通过迭代器遍历:"  <<endl;  
  39.     for(string::iterator it = s1.begin(); it!= s1.end(); it++)  
  40.     {  
  41.         cout << *it << " ";  
  42.     }  
  43.     cout << endl;  
  44.   
  45.     //通过at()方式遍历  
  46.     cout << "3、通过at()方式遍历:"  <<endl;  
  47.     for (int i = 0; i < s1.length(); i++)  
  48.     {  
  49.         cout << s1.at(i) << " ";        //此方式可以在越界时抛出异常  
  50.     }  
  51.     cout << endl;  
  52. }  
  53.   
  54. //字符指针和字符串的转换  
  55. void strConvert()  
  56. {  
  57.     cout << "字符指针和字符串的转换:"  <<endl;  
  58.     string s1 = "abcdefg";  //初始化字符串  
  59.   
  60.     cout << "string转换为char*:"  <<endl;  
  61.     //string转换为char*  
  62.     cout << s1.c_str() <<endl;  //s1.c_str()即为s1的char *形式  
  63.   
  64.     cout << "char*获取string内容:"  <<endl;  
  65.     //char*获取string内容  
  66.     char buf[64] = {0};  
  67.     s1.copy(buf, 7);//复制7个元素  
  68.     cout << buf <<endl;  
  69. }  
  70.   
  71. //字符串连接  
  72. void strAdd()  
  73. {  
  74.     cout << "字符串连接:"  <<endl;  
  75.   
  76.     cout << "方式1:"  <<endl;  
  77.     string s1 = "123";  
  78.     string s2 = "456";  
  79.     s1 += s2;  
  80.     cout << "s1 = "<< s1 << endl;  
  81.   
  82.     cout << "方式2:"  <<endl;  
  83.     string s3 = "123";  
  84.     string s4 = "456";  
  85.     s3.append(s4);  
  86.     cout << "s3 = "<< s3 << endl;  
  87. }  
  88. int main()  
  89. {  
  90.     //初始化  
  91.     strInit();  
  92.     cout << endl;  
  93.     //遍历  
  94.     strErgo();  
  95.     cout << endl;  
  96.     //字符指针类型和字符串转换  
  97.     strConvert();  
  98.     cout << endl;  
  99.     //字符串连接  
  100.     strAdd();  
  101.     cout << endl;  
  102.     system("pause");  
  103.     return 0;  
  104. }  

程序运行结果:

[plain]  view plain  copy
 
  1. 字符串初始化:  
  2. s1 = abcdefg  
  3. s2 = abcdefg  
  4. s3 = abcdefg  
  5. s4 = sssssss  
  6.   
  7. 字符串遍历:  
  8. 1、通过数组方式遍历:  
  9. a b c d e f g  
  10. 2、通过迭代器遍历:  
  11. a b c d e f g  
  12. 3、通过at()方式遍历:  
  13. a b c d e f g  
  14.   
  15. 字符指针和字符串的转换:  
  16. string转换为char*:  
  17. abcdefg  
  18. char*获取string内容:  
  19. abcdefg  
  20.   
  21. 字符串连接:  
  22. 方式1:  
  23. s1 = 123456  
  24. 方式2:  
  25. s3 = 123456  
  26.   
  27. 请按任意键继续. . .  

相关文章:

  • Java设计模式圣经连载(03)-抽象工厂模式
  • Centos中文乱码问题的解决。
  • Python函数知识汇总-课堂笔记
  • AD与DNS集成,且有备份AD与DNS,主AD与DNS坏,备份DNS如何成为主
  • SubSonic3.0使用外连接查询时查询不出数据的问题修改
  • 海量文件拷贝(Windows/Linux)
  • VS.net和Reflector 图标解释
  • EPEL源
  • centos7 修改mac地址
  • c# 4.0新特性一览
  • 荣光医院医道会比赛策略
  • CSS布局模型 之 浮动模型(浮动的工作原理和清除浮动技巧?)
  • sql优化方案,总结的比较全面
  • 在此之前的软件系统做开发—需求的研究框架
  • 【安全牛学习笔记】字典、在线密码破解-hydra
  • 《剑指offer》分解让复杂问题更简单
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • javascript数组去重/查找/插入/删除
  • SpiderData 2019年2月13日 DApp数据排行榜
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • 反思总结然后整装待发
  • 分布式熔断降级平台aegis
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 译有关态射的一切
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • #13 yum、编译安装与sed命令的使用
  • #传输# #传输数据判断#
  • #在 README.md 中生成项目目录结构
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (2)(2.10) LTM telemetry
  • (八)c52学习之旅-中断实验
  • (补)B+树一些思想
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • *** 2003
  • ***测试-HTTP方法
  • .net mvc 获取url中controller和action
  • .NET 中创建支持集合初始化器的类型
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .NET开发不可不知、不可不用的辅助类(一)
  • .NET开源项目介绍及资源推荐:数据持久层
  • 。Net下Windows服务程序开发疑惑
  • /etc/fstab 只读无法修改的解决办法
  • @Autowired和@Resource装配
  • @ConfigurationProperties注解对数据的自动封装
  • @EnableAsync和@Async开始异步任务支持
  • @Transaction注解失效的几种场景(附有示例代码)
  • [1181]linux两台服务器之间传输文件和文件夹
  • [ACL2022] Text Smoothing: 一种在文本分类任务上的数据增强方法
  • [AMQP Connection 127.0.0.1:5672] An unexpected connection driver error occured
  • [android]-如何在向服务器发送request时附加已保存的cookie数据
  • [BZOJ3757] 苹果树
  • [C#]winform制作仪表盘好用的表盘控件和使用方法
  • [C++]类和对象【下】