当前位置:网站首页>string类接口介绍及应用
string类接口介绍及应用
2022-08-11 07:13:00 【安河桥畔】
string类接口
前言:STL介绍
STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架
注:本文主要介绍string类的常用接口,并非所有
一、构造方法
常用构造方法:
string s1();//构造空对象
string s2("hello world");//用c_string构造string类对象
string s3(3, 'w');//多个的字符构造
string s4(s3);//拷贝构造
二、容量操作
- size 返回字符串有效字符长度
- lenth 返回字符串有效字符长度
size与lenth作用相同,区别是lent只能用于string类 - capacity 返回空间总大小
- empty 检测字符串释放为空串,是返回true,否则返回false
- clear 清空有效字符
- reserve 为字符串预留空间
- resize 将有效字符的个数该成n个,多出的空间用字符c填充
resize
resize:改变有效字符的个数,如果空间不足会进行扩容
两种常用方法
void resize (size_t n);
void resize (size_t n, char c);
示例:
void Test()
{
string s1(3, 'w');
s1.resize(5, 'h');
}
注意:resize可以增大也可以减小字符个数,如图:
reserve扩容机制
环境:VS2022
环境:CentOS 7.6
rerserve有自己内部的扩容机制,与编译环境有关,在VS2022中为1.5倍扩容,Linux环境下是2倍扩容。向string类对象中插入元素时,会频繁调用reserve方法进行扩容,所以在实际使用中,如果知道要插入元素的个数,应该提前调用reserve预留空间,提高程序运行效率。
在VS中,string类大小(Win32):
每个string类对象会在底层开辟一个16字节的字符数组,字符串中的内容较少时都保存在这个数组中。
clear的用法
调用clear方法会清空当前对象中的内容
三、元素访问
operator[]: 返回pos位置的字符,const string类对象调用
begin+ end begin:获取第一个字符的迭代器 ,end获取最后一个字符下一个位置的迭代器
rbegin + rend :rbegin获取最后一个字符的迭代器 , rend获取第一个字符前一个位置的迭代器
范围for :C++11支持更简洁的范围for的新遍历方式
下标访问
string s1("Hello");
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] ;
}
cout << endl;
范围for
for (auto ch : s1)
{
cout << ch ;
}
cout << endl;
迭代器
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it;
it++;
}
cout << endl;
反向迭代器
//auto rit = s1.rbegin();//也可以用auto定义,更加便捷
string::reverse_iterator rit = s1.rbegin();
while (rit != s1.rend())
{
cout << *rit;
rit++;
}
cout << endl;
四、类对象修改操作
这一类接口主要是修改对象的内容
operator+=
可以用于拼接char*类型字符串,string类对象,或者字符
- 用"+="拼接两个string类对象
- 拼接string类对象和const char*类型字符串
erase
删除部分字符串,减少其长度
- erase三种用法
- erase对size和capacity的影响,只改变size,不会减小capacity
swap
交换两个string类对象的内容
assign
assign为一个以已经定义的string类对象重新分配内容,assign不会改变对象的capacity
insert
insert可以进行任意位置的插入操作
replace
代替string类对象某一段区间中的内容
五、string类操作
find&rfind
在一个string类对象中查找一个字符或者字符串,并返回其首次出现的位置,如果没有找到就返回npos,即size_t类型的最大值。rfind与find功能相似,但是rfind是从尾至头查找的。
将s1中的thought替换成will:
void Test()
{
string s1("Where there is a thought there is a way.");
string s2("thought");
s1.replace(s1.find(s2), s2.length(), "will");
}
结果:
substr
截取字串,第一个参数表示开始截取的位置,第二个参数表示要截取的长度,如果第二个参数不指定,则默认截取到字符串末尾
函数原型:
string substr (size_t pos = 0, size_t len = npos) const;
应用
- 截取文件后缀
- 域名获取
compare
与C语言中的strcmp作用相同,但是compare适用于string类对象的比较,返回0表示相等,返回值小于0表示当前对象比要比较的对象小,返回值大于0表示当前对象比要比较的对象大。
对于string类对象,可以直接用’>‘’<‘和’='进行比较
六、string类非成员函数
getline
函数原型:
istream& getline (istream& is, string& str, char delim);
istream& getline (istream& is, string& str);
getline第一个参数为输入流对象,第二个参数是要输入到的string类对象,第三个参数是输入终止符,如果不指定则默认为遇到’\0’终止。geline可用于整行字符串的输入。
swap
区分这个swap与string类成员函数中的swap
边栏推荐
猜你喜欢
1036 Programming with Obama (15 points)
The growth path of a 40W test engineer with an annual salary, which stage are you in?
1056 Sum of Combinations (15 points)
基于微信小程序的租房小程序
3.2 - classification - Logistic regression
tf中自减操作;tf.assign_sub()
Keep track of your monthly income and expenses through bookkeeping
语音信号处理:预处理【预加重、分帧、加窗】
无服务器+域名也能搭建个人博客?真的,而且很快
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
随机推荐
2022-08-10 mysql/stonedb-慢SQL-Q16-耗时追踪
【LeetCode】链表题解汇总
我的创作纪念日丨感恩这365天来有你相伴,不忘初心,各自精彩
查询跟踪快递单号物流,智能分析物流中转有延误的单号
2022-08-09 Group 4 Self-cultivation class study notes (every day)
What are the things that should be planned from the beginning when developing a project with Unity?How to avoid a huge pit in the later stage?
Pico neo3在Unity中的交互操作
详述 MIMIC护理人员信息表(十五)
Item 2 - Annual Income Judgment
1036 跟奥巴马一起编程 (15 分)
Active users of mobile banking grew rapidly in June, hitting a half-year high
租房小程序
One-hot in TF
Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
1101 How many times B is A (15 points)
4.1-支持向量机
1002 Write the number (20 points)
【LaTex-错误和异常】\verb ended by end of line.原因是因为闭合边界符没有在\verb命令所属行中出现;\verb命令的正确和错误用法、verbatim环境的用法
囍楽云任务源码
tf中自减操作;tf.assign_sub()