当前位置:网站首页>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
边栏推荐
- 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?
- Pytorch模型转ONNX模型
- 接口测试的基础流程和用例设计方法你知道吗?
- leetcode: 69. Square root of x
- JRS303-Data Verification
- The easiest trick to support quick renaming of various files
- 1061 True or False (15 points)
- 4.1-支持向量机
- Pico neo3 Unity Packaging Settings
- Depth (relay supervision)
猜你喜欢
【LeetCode】链表题解汇总
tf.reduce_mean()与tf.reduce_sum()
Implementation of FIR filter based on FPGA (5) - FPGA code implementation of parallel structure FIR filter
关于Excel实现分组求和最全文档
[Recommender System]: Overview of Collaborative Filtering and Content-Based Filtering
TF中的One-hot
3.1-Classification-probabilistic generative model
经典论文-MobileNet V1论文及实践
通过记账,了解当月收支情况
Interaction of Pico neo3 in Unity
随机推荐
1046 punches (15 points)
1.2-误差来源
【Pytorch】nn.PixelShuffle
1061 True or False (15 points)
Dynamic Agent Learning
TF中的条件语句;where()
1106 2019数列 (15 分)
Activity的四种状态
租房小程序
2022 China Soft Drink Market Insights
6月各手机银行活跃用户较快增长,创半年新高
TF中使用softmax函数;
【latex异常和错误】Missing $ inserted.<inserted text>You can‘t use \spacefactor in math mode.输出文本要注意特殊字符的转义
3.1-分类-概率生成模型
Pico neo3 Unity打包设置
1051 Multiplication of Complex Numbers (15 points)
2022年中国软饮料市场洞察
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
1096 大美数 (15 分)
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices