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

C++ STL学习之string(一)

默认成员函数之构造函数

void test_string1() { string s1; // 构造函数 string s2("hello"); // 带参构造 string s3("hello", 2); // 只拷贝前两个字符 string s4(s2); // 拷贝构造 string s5(s2, 1, 2); // 从第1个字符开始,拷贝两个字符 string s6 = s2; // 拷贝构造,在赋值过程中调用了拷贝构造,s6 在定义时用 s2 初始化,调用拷贝构造 string s7; s7 = s2; // s7 先定义(默认构造),再赋值,调用的是拷贝赋值运算符 operator= cout << s1 << endl; cout << s2 << endl; cout << s3 << endl; cout << s4 << endl; cout << s5 << endl; }

输出

string的遍历方法

1、for循环遍历

void test_string2() { string s1("hello"); s1 += " "; s1 += "world"; for (size_t i = 0; i < s1.size(); ++i) { cout << s1[i] << " "; } cout << endl; }

2、迭代器遍历

void test_string2() { string s1("hello"); s1 += " "; s1 += "world"; // 迭代器 string::iterator it = s1.begin(); while (it != s1.end()) { cout << *it << " "; ++it; } cout << endl; }

3、范围for遍历(C++11)

void test_string2() { string s1("hello"); s1 += " "; s1 += "world"; // 范围for c++11 for (auto ch : s1) { cout << ch << " "; } cout << endl; }

4、反向迭代器,反向遍历

void test_string3() { string s1("hello world"); string::reverse_iterator rit = s1.rbegin(); while (rit != s1.rend()) { cout << *rit << " "; ++rit; } cout << endl; }

输出结果为:d l r o w o l l e h

string转换成整数类型

int string2num(const string& nums) { int val = 0; string::const_iterator vit = nums.begin(); while (vit != nums.end()) { val *= 10; val += (*vit - '0'); ++vit; } return val; }

上述代码中使用了const迭代器,只能读,不能写,*vit的值不能再改变

string的插入删除

void test_string4() { string s1; s1.push_back('x'); // 尾插,在尾部插入一个字符,输出:x s1.append("11111"); // 尾部插入字符串,输出:x11111 s1 += "yyy"; // 尾部插入字符串,输出:x11111yyy string s; s += '1'; s += "3456"; cout << s << endl; // 输出:13456 s.insert(s.begin(), '0'); // 头插,在1之前插入0 cout << s << endl; // 输出:013456 s.insert(2, "2"); // 在2的位置插入2 cout << s << endl; // 输出:0123456 s.erase(2, 10); // 从第2个位置开始删除 cout << s << endl; // 输出:01 }

string的查找find()函数用法

查找文件后缀名

void test_string5() { string s1("string.cpp"); string s2("string.c"); string s3("string.txt"); size_t pos1 = s1.find('.'); if (pos1 != string::npos) { cout << s1.substr(pos1) << endl; } size_t pos2 = s2.find('.'); if (pos2 != string::npos) { cout << s2.substr(pos2) << endl; } size_t pos3 = s3.find('.'); if (pos3 != string::npos) { cout << s3.substr(pos3) << endl; } }

string中getline()的用法

string s;
cin >> s; 遇到空格或者换行就回结束
而getline(cing, s) 则只会遇到换行才结束

http://www.jsqmd.com/news/1256209/

相关文章:

  • YuukiPS Launcher-PC:3个实用技巧快速掌握多游戏启动工具
  • HarmonyOS开发实战:小分享-列表性能优化——组件复用与异步渲染
  • 2026年AI降重工具怎么选?4款主流工具横向测评,看完不踩坑
  • Chrome全屏截图终极解决方案:一键保存完整网页的免费插件
  • AIAgent开发实战:框架选型与工程化落地指南
  • 高效离线语音识别终极指南:TMSpeech从入门到精通
  • 中兴光猫工厂模式解锁:3分钟掌握专业级网络管理权限
  • Chrome全屏截图插件:一键保存完整网页的终极解决方案
  • 中兴光猫终极解锁指南:3步免费开启完整管理权限
  • 2026年中铜陵特色小吃培训速成班盘点与机构推荐 - 装修教育财税推荐2026
  • Hermes Agent MCP 接入:一条配置连上外部工具,但别把权限全交出去
  • 【正则生成黄金标准】:IEEE最新白皮书认证的5大评估维度,92%开发者从未用过!
  • 珠三角光学电子精密五金塑料泡壳定制解析|透明吸塑泡壳、底盖通用吸塑盒专业工厂技术盘点 - 变量人生001
  • 2026年北京员工福利定制厂家推荐:员工福利定制品牌选择指南 - 全域品牌推荐
  • 如何彻底告别AWCC臃肿:AlienFX Tools终极轻量级Alienware控制指南
  • 免费解锁网易云音乐NCM文件:ncmdumpGUI完整实战指南
  • 从零开始:增值税发票OCR识别的最小可运行示例
  • 基于对比学习的RAW图像去噪技术解析与应用
  • 139、锐化与边缘增强算法:非锐化掩模、拉普拉斯与自适应增益控制
  • 已认证小程序变更主体干嘛用?手机线上申办流程攻略 - 跑政通
  • 猫抓视频嗅探工具:如何轻松下载网页视频的完整指南
  • 2026年7月美的空调售后服务电话24小时人工受理400维修热线升级公示 - 优企名品
  • 2026北京表主避坑实录:亲身经历三家门店,拆解高报低收全流程 - 日常比对手册
  • Java AI 微服务冷启动优化:GraalVM 原生镜像让我的 Spring Boot 3 应用快了 50 倍
  • AMD锐龙终极调试指南:30分钟掌握硬件性能调优神器
  • 5分钟快速上手:RTL8852BE Wi-Fi 6驱动完整安装与优化指南
  • 抖音用户公开信息API:一个最小可运行的curl示例
  • Unity微信小游戏性能优化实战:包体瘦身与内存管理
  • c++教程3:基本运算符的使用
  • Full Page Screen Capture:告别网页截图烦恼的终极解决方案