引言

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

C++ STL中的string区别于C语言风格的cstring, 对于这一点有疑惑的小伙伴推荐先看一下这篇文章:
C++笔记(cstring和string的区别)

2.2.1 string的特性

  1. Char为指针,String是一个类。string封装了char,管理这个字符串,是一个char*型的容器。

  2. 不用考虑内存释放和越界

  3. String封装了很多实用的成员方法
    查找,拷贝,删除…

2.2.2 string用法理论

2.2.2.1 string构造函数

string(); //创造一个空的字符串
string(const char*s) //使用字符串s初始化
string(const string& str); //使用一个string对象初始化另一个string对象
string(int n, char c); //使用n个字符c初始化

2.2.2.2 string赋值操作

赋值的函数原型:

string& operator=(const char* s); //char*类型字符串赋值给当前的字符串
string& operator=(const string &s); //把字符串s赋给当前字符串
string& operator=(char c); //字符赋值给当前的字符串
string& assign(const char *s); //把字符串s赋给当前字符串
string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前字符串
string& assign(const string &s); //把字符串s赋给当前字符串
string& assign(int n, char c); //用n个字符c赋给当前字符串

2.2.2.3 string取值操作

这里参考下面代码区text03函数

string s = "abcdefg";
//重载[]操作符
for (int i = 0;i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
//at成员函数
for (int i = 0; i < s.size(); i++)
{
cout << s.at(i) << " ";
}
cout << endl;

2.2.2.4 string拼接操作

string& operator+=(const char* str); //重载+=操作符
string& operator+=(const char c); //重载+=操作符
string& operator+=(const string& str); //重载+=操作符
string& append(const char *s); //把字符串s连接到当前字符串结尾
string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s); //同operator+=(const string& str)
string& append(const string &s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾

2.2.2.6 string查找

//string查找
int find(const string& str, int pos=0) const; //查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos=0) const; //查找s第一次出现位置,从pos开始查找
int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置
int find(const char c, int pos=0) const; //查找字符c第一次出现位置
int rfind(const string& str, int pos=npos) const; //查找str最后一次位置,从pos开始查找
int rfind(const char* s, int pos=npos) const; //查找s最后一次出现位置,从pos开始查找
int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置
int rfind(const char* c, int pos=0) const; //查找字符c最后一次出现位置

2.2.2.7 string替换

//string替换
string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s

2.2.2.7 string字符串比较

字符串比较是按字符的ASCII码进行对比或者说是按照字典序,字母越靠前越小。
注意:大写A比小写a小

(等于) = 返回 0

(大于) > 返回 1

(小于) < 返回 -1

int compare(const string &s) const; // 与字符串s比较
int compare(const char *s) const; //与字符串s比较

2.2.2.8 string字符串插入和删除

string& insert(int pos, const char* s); //插入字符串
string& insert(int pos, const string& str); //插入字符串
string& insert(int pos, int n, char c); //在指定位置插入n个字符c
string& erase(int pos, int n=npos); //删除从pos开始的n个字符

2.2.2.9 string子串

string substr(int pos=0, int n=npos) const; //返回由pos开始的n个字符组成的字符串

string用法示例代码

//函数text0~9对应2.2.2.1~2.2.2.9
#include<iostream>
#include<string>
using namespace std; //初始化
void text01()
{
cout << "\ntext01\n";
string s1;//调用无参构造
string s2(10, 'a');
string s3("abcdefg");
string s4(s3);//拷贝构造 cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
} //赋值操作
void text02()
{
cout << "\ntext02\n";
string s1;
string s2("appp");
s1 = "abcdef";
cout << s1 << endl;
s1 = s2;
cout << s1 << endl; //成员方法
s1.assign("jkl");
cout << s1 << endl;
} //取值操作
void text03()
{
cout << "\ntext03\n";
string s1 = "abcdefg"; //重载[]操作符
for (int i = 0;i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl; //at成员函数
for (int i = 0; i < s1.size(); i++)
{
cout << s1.at(i) << " ";
}
cout << endl; //区别:[]方式 如果访问越界,直接挂了
//at方式 访问越界 抛异常out_of_range
try
{
/*cout << s1[100] << endl;*/
cout << s1.at(100) << endl;
}
catch (...)
{
cout << "越界" << endl;
}
} //拼接操作
void text04()
{
cout << "\ntext04\n";
//1
string s = "abcd";
s += "abcd";
string s2 = "1111";
s += s2;
cout << s << endl; //2
string s3 = "2222";
s2.append(s3);
cout << s2 << endl;
string s4 = s2 + s3;
cout << s4 << endl;;
} //查找操作
void text05()
{
cout << "\ntext05\n";
string s = "abcdefghjkl";
//查找第一次出现的位置
int pos = s.find("fg");
cout << "pos:" << pos << endl;//返回5 //查找最后一次出现的位置
pos = s.rfind("fg");
cout << "pos:" << pos << endl;
} //替换操作
void text06()
{
cout << "\ntext06\n";
string s = "abcdefg";
s.replace(1,3,"111");
cout << s << endl;
} //字符串比较
void text07()
{
cout << "\ntext07\n";
string s1 = "abcd", s2 = "abce";
if (s1.compare(s2) == 0)
{
cout << "相等" << endl;
}
else
{
cout << "不相等 值为:" << s1.compare(s2) << endl;
//ASCII码字典序,小于为-1。
}
} //string子串
void text08()
{
cout << "\ntext08\n";
string s = "abcdefg";
string mysubstr = s.substr(1, 3);
cout << mysubstr << endl;
} //string插入和删除操作
void text09()
{
cout << "\ntext09\n";
string s = "abcdefg";
s.insert(3, "111");
cout << s << endl; s.erase(0, 2);//从0位置开始删2个元素
cout << s << endl;
} int main()
{
text01();
text02();
text03();
text04();
text05();
text06();
text07();
text08();
text09();
return 0;
}

string用法示例代码结果


谢谢阅读(〃’ ▽ '〃)如有纰漏欢迎指出,觉得还不错就点个赞吧。

2.2 C++STL string容器详解的更多相关文章

  1. C++ STL bitset 容器详解

    C++ STL bitset 容器详解 本篇随笔讲解\(C++STL\)中\(bitset\)容器的用法及常见使用技巧. \(bitset\)容器概论 \(bitset\)容器其实就是个\(01\)串 ...

  2. 2.3 C++STL vector容器详解

    文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...

  3. 2.7 C++STL list容器详解

    文章目录 2.7.1 引入 2.7.2代码示例 2.7.3代码运行结果 总结 2.7.1 引入 STL list 容器,又称双向链表容器,即该容器的底层是以双向链表的形式实现的.这意味着,list 容 ...

  4. 2.4 C++STL deque容器详解

    文章目录 2.4.1 引入 2.4.2 代码示例 2.4.3 代码运行结果 2.4.4 具体案例 总结 2.4.1 引入 deque容器类比vector容器来学习. deque为双向开口容器,见下图. ...

  5. C++之string类型详解

    C++之string类型详解 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个泛型类出现,他集成的操作函 ...

  6. [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。

    一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...

  7. STL bind1st bind2nd详解

    STL bind1st bind2nd详解   先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...

  8. 【转】Java魔法堂:String.format详解

    Java魔法堂:String.format详解     目录     一.前言    二.重载方法     三.占位符     四.对字符.字符串进行格式化     五.对整数进行格式化     六. ...

  9. Java String类详解

    Java String类详解 Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,很多时候,我们对它既熟悉又陌生. 类结构: public final ...

随机推荐

  1. iptables防火墙 (纸是包不住火的,得用水泥)

    iptables防火墙 1.Linux防火墙基础 2.编写防火墙规则 1.Linux防火墙基础 iptables概述: Linux 系统的防火墙: IP信息包过滤系统,它实际上由两个组件netfilt ...

  2. JS快速入门(一)

    目录 Javascript快速入门(一) 变量的命名规则 变量与常量 变量 常量 数据类型 数值型:Number 字符串:String 常用方法: 布尔类型:Boolean 未定义类型 :Undefi ...

  3. ajax请求egg用nginx转发跨域问题

    火狐浏览器报的 谷歌浏览器报的 前提: npm i egg-cors --save config 文件下的pulgin.js 已经添加 //启用跨域支持 exports.cors = { enable ...

  4. C++职工管理系统

    目录 职工管理系统 一. 需求 二. 创建管理类 1.创建文件 2. 头文件实现 3. 源文件实现 三. 菜单功能 1. 添加成员函数 2. 功能实现 3. 测试菜单功能 四. 退出功能 1. 提供功 ...

  5. Solution -「CF 232E」Quick Tortoise

    \(\mathcal{Description}\)   Link.   在一张 \(n\times m\) 的网格图中有空格 . 和障碍格 #,\(q\) 次询问,每次查询从 \((x_1,y_1)\ ...

  6. Solution -「CF 917D」Stranger Trees

    \(\mathcal{Description}\)   Link.   给定一棵包含 \(n\) 个点的有标号树,求与这棵树重合恰好 \(0,1,\cdots,n-1\) 条边的树的个数,对 \(10 ...

  7. Java全栈学习路线、学习资源和面试题一条龙

    肝了一个月,终于把Java学习路线.面试题资源和电子书资源都整理好了. Java 从基础到微服务的学习路线,其中还包括科班知识.学习建议.后续的学习引导和相应的学习资源(视频.书籍.网站),还整理了J ...

  8. 使用PHP利用phpmailer发送电子邮件

    先来几句废话:    phpMailer是一个非常强大的php发送邮件类,可以设定发送邮件地址.回复地址.邮件主题.html网页,上传附件,并且使用起来非常方便.     phpMailer的特点:  ...

  9. 数据库项目部署(nginx)

    1.在虚拟机2008server下载nginx http://nginx.org/download/nginx-1.17.10.zip 注1:此版本为window版本 linux版本 2. 解压软件至 ...

  10. VS Code 在线运行:code-server部署(系列一)

    偶然间发现VS Code可以在线运行,闲来无事部署了一套试试效果,先上图,是不是很熟悉的感觉,初步体验基本跟本地运行效果一样. 进入正题 首先要准备一套linux环境,环境配置最低是1G内存.2核CP ...