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

STL的string容器

string基本概念

string是C++风格的字符串,本质上是一个类。

string 和 char* 的区别

char* 是一个指针;

string是一个类,内部封装了 char* ,用来管理字符串,是一个 char* 型的容器。

特点

string内部封装了很多成员函数

例如:查找find、拷贝copy、删除delete、替换replace、插入insert

string管理 char* 分配的内存,不用担心复制越界和取值越界等,由类内部负责。

string的构造函数

#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <algorithm> // 使用STL提供的遍历算法,遍历vector容器using namespace std;void test()
{// 默认构造string s1 = "Hello World";cout << "s1 = " << s1 << endl;// 第二种构造函数const char* str = "Hello World";string s2(str);cout << "s2 = " << s2 << endl;// 第三种构造函数string s3(s2);cout << "s3 = " << s3 << endl;// 第四种构造函数string s4(10, 'A');cout << "s4 = " << s4 << endl;
}int main(void)
{test();system("pause");return 0;
}

运行截图:

 string赋值操作

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{// 第一种赋值方式string s1 = "Hello World";cout << "s1 = " << s1 << endl;// 第二种赋值方式string s2;s2 = s1;cout << "s2 = " << s2 << endl;// 第三种赋值方式string s3;s3 = 'A';cout << "s3 = " << s3 << endl;// 第四种赋值方式string s4;s4.assign("Hello C++");cout << "s4 = " << s4 << endl;//第五种赋值方式string s5;s5.assign("Hello C++", 5);cout << "s5 = " << s5 <<endl;// 第六种赋值方式string s6;s6.assign(s5);cout << "s6 = " << s6 <<endl;// 第七种赋值方式string s7;s7.assign(10, 'W');cout << "s7 = " << s7 << endl;}int main(void)
{test();system("pause");return 0;
}

运行截图:

string字符串拼接

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{// 第一种字符串拼接方式string str1 = "Hello";str1 += " C++";cout << str1 << endl;// 第二种字符串拼接方式str1 += '!';cout << str1 << endl;// 第三种字符串拼接方式string str3 = " Good morning";str1 += str3;cout << str1 << endl;// 第四种字符串拼接方式string str4 = "I";str4.append(" Love ");cout << str4 << endl;// 第五种字符串拼接方式string str5 = str4;str5.append("C++ and Java", 3);cout << str5 << endl;// 第六种字符串拼接方式string str6 = "你好:";str6.append(str1);cout << str6 << endl;// 第七种字符串拼接方式string str7;str7.append(str1, 0, 9);cout << str7 << endl;}int main(void)
{test();system("pause");return 0;
}

运行截图:

 

string字符串查找和替换

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;// 字符串查找
void test()
{string s1 = "Hello World";int pos = s1.find("Hello");if (pos == -1){cout << "未找到字符串" << endl;}else{cout << "pos = " << pos << endl; // pos = 0}
}// 字符串替换
void test02()
{string str = "Hello World";str.replace(6, 5, "C++");cout << str << endl;
}int main(void)
{test();test02();system("pause");return 0;
}

运行截图:

 

string字符串比较

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{string s1 = "Hello World";string s2 = "Hello";if (s1.compare(s2) == 0){cout << "字符串相等" << endl;}else{cout << "字符串不等" << endl;}
}int main(void)
{test();system("pause");return 0;
}

运行截图:

 

string字符串存取

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{string s = "Hello World";// 1、通过[]访问单个字符for (int i = 0; i < s.size(); i++){cout << s[i] << "  ";}cout << endl;// 2、通过at访问单个字符for (int i = 0; i < s.size(); i++){cout << s.at(i) << "  ";}cout << endl;}int main(void)
{test();system("pause");return 0;
}

运行截图:

 

string子串获取

#include <iostream>
#include <Windows.h>
#include <string>using namespace std;void test()
{string s1 = "Hello World";string s2 = s1.substr(6, 5);cout << s2 << endl; // 输出World
}int main(void)
{test();system("pause");return 0;
}

运行截图:

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

相关文章:

  • 半导体工艺技术
  • acwing算法提高之图论--单源最短路的扩展应用
  • SQLServer数据库使用Function实现根据字段内容的拼音首字母进行数据查询
  • Linux——信号概念与信号产生方式
  • 赋值语句还能当判断条件?涨芝士了!
  • 数据结构 - 算法效率|时间复杂度|空间复杂度
  • 接口自动化之 + Jenkins + Allure报告生成 + 企微消息通知推送
  • 『Apisix安全篇』探索Apache APISIX身份认证插件:从基础到实战
  • 【01-20】计算机网络基础知识(非常详细)从零基础入门到精通,看完这一篇就够了
  • 『大模型笔记』常见的分布式并行策略(分布式训练)
  • java 企业工程管理系统软件源码+Spring Cloud + Spring Boot +二次开发+ 可定制化
  • 3D数据格式导出工具HOOPS Publish如何生成高质量3D PDF?
  • 【springboot】闲话 springboot 的几种异步机制 及 长轮询的概念和简单实现
  • Mysql---安全值守常用语句
  • containerd快速安装指南
  • Javascript - 正则表达式相关的一些基础的范例
  • JUC:线程活跃性(死锁、活锁、饥饿)
  • RGB到灰度图像的转换原理及例程
  • PCA+DBO+DBSCN聚类,蜣螂优化算法DBO优化DBSCN聚类,适合学习,也适合发paper!
  • 创建数据库与表单以及管理表单和数据
  • Milvus+ATTU环境搭建
  • Python 基于 OpenCV 视觉图像处理实战 之 OpenCV 简单实战案例 之八 简单水彩画效果
  • Chrome浏览器 安装Vue插件vue-devtools
  • 相册清理大师-手机重复照片整理、垃圾清理软件
  • 【GitLab】Ubuntu 22.04 快速安装 GitLab
  • Linux重点思考(下)--shell脚本使用以及内核开发
  • 2024世界技能大赛某省选拔赛“网络安全项目”B模块--应急响应解析
  • 苹果与百度合作,将在iPhone 16中使用生成式AI
  • java中的单例模式
  • pytorch笔记篇:pandas之数据预处理(更新中)