当前位置:网站首页>初识string+简单用法(二)
初识string+简单用法(二)
2022-07-02 03:35:00 【i跑跑】
这篇文章我们来接着看看string还有哪一些常用的方式:
目录
insert--指定位置插入
插入字符(1)
string& insert (size_t pos, size_t n, char c);
pos为指定位置,n为字符个数,c代表要插入的字符
简单演示一下:
插入字符(2)
iterator insert (iterator p, char c);
指向插入一个字符,不考虑字符个数,那么就可以使用参数为(迭代器,字符)的函数:
简单示例:
插入字符串
string& insert (size_t pos, const char* s);
简单演示:
erase--指定位置删除
删除字符
iterator erase (iterator p);
简单演示:
删除指定字符串
string& erase (size_t pos = 0, size_t len = npos);
简单演示:
这其中len默认等于npos,npos是一个静态成员变量:
当没有给第二个参数len明确的值时,会从起始位置一直删除到字符串结束。
当给定的len值大于原本字符串的长度时,和len不赋值效果相同,一直删到字符串结束为止。
swap--string类型的交换
string类中的swap
void swap (string& str);
交换的是两个对象的指针,指针指向的内容改变,交换效率高:
全局函数swap
是内容的交换,但因为是自定义类型,会涉及到深拷贝问题,效率低,开销大:
c_str--返回数组指针
相当于将string类型变成我们更加熟悉的char*类型:
简单演示:
int main()
{
string s1("hello");
cout << typeid(s1).name() << endl;
cout << typeid(s1.c_str()).name() << endl;
return 0;
}
find--指定位置查找
查找字符
size_t find (char c, size_t pos = 0) const;
指定位置查找字符,返回匹配到的字符的下标位置,否则返回npos--整形最大值
查找字符串
size_t find (const char* s, size_t pos = 0) const;
指定起始位置查找与之匹配的子串,返回子串开头下标位置否则返回npos
从下标为6的位置开始找,匹配到字符串,返回字符串的下标位置
rfind--从后往前查找
和find功能相同,只是找的起始方向不同,一个前一个后,还是很容易理解的
substr--取子串
string substr (size_t pos = 0, size_t len = npos) const;
pos为起始位置,len为子串长度,返回这个子串
举例子:
没有找到会返回npos,可以当作判断条件来使用
int main()
{
string s1("xxxxxxxxhelloaaaaa");
size_t end = s1.rfind('o');
size_t pos = s1.find("hello");
if(s1.find("hello")!=string::npos)
{
cout << s1.substr(pos, end - pos+1) << endl;
}
return 0;
}
getline--获取一行
不会受到空格的影响:
当我们用 cin 输入一行字符串时,若中间有空格,会停止获取空格后的字符串:
这时可以使用getline,用法:
istream& getline (istream& is, string& str);
string的常见用法到这里就结束了,其他用法感兴趣的伙伴可以查阅文档自主学习,推荐网站:
希望和大家一起进步!!!
边栏推荐
- VS2010插件NuGet
- JS generate random numbers
- Kotlin basic learning 13
- Oracle common SQL
- How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
- Unity脚本的基础语法(6)-特定文件夹
- Aaaaaaaaaaaa
- Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
- This article describes the step-by-step process of starting the NFT platform project
- [untitled] basic operation of raspberry pie (2)
猜你喜欢
蓝桥杯单片机省赛第十二届第二场
蓝桥杯单片机省赛第十二届第一场
Download and use of the super perfect screenshot tool snipaste
蓝桥杯单片机省赛第十一届
[designmode] builder model
Getting started with MQ
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
[yolo3d]: real time detection of end-to-end 3D point cloud input
随机推荐
JS generate random numbers
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
[designmode] builder model
Kotlin基础学习 14
[HCIA continuous update] overview of dynamic routing protocol
FFMpeg AVFrame 的概念.
Unity脚本的基础语法(7)-成员变量和实例化
venn圖取交集
Oracle common SQL
ImageAI安装
一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
蓝桥杯单片机数码管技巧
蓝桥杯单片机省赛第十一届
Knowing things by learning | self supervised learning helps improve the effect of content risk control
What do you know about stock selling skills and principles
MySQL index, transaction and storage engine
IPhone 6 plus is listed in Apple's "retro products" list
Kotlin basic learning 15
Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share