当前位置:网站首页>First acquaintance with string+ simple usage (II)
First acquaintance with string+ simple usage (II)
2022-07-02 03:43:00 【I run】
Let's take a look at this article string What other common ways :
Catalog
insert-- Insert... At specified location
erase-- Delete... In the specified location
find-- Specify location lookup
insert-- Insert... At specified location
Insert characters (1)
string& insert (size_t pos, size_t n, char c);
pos Specify the location for ,n Is the number of characters ,c Represents the character to be inserted
Just to show you :

Insert characters (2)
iterator insert (iterator p, char c);Point to insert a character , Regardless of the number of characters , Then you can use the parameter ( iterator , character ) Function of :
A simple example :

Insert string
string& insert (size_t pos, const char* s);
Simple demonstration :

erase-- Delete... In the specified location
Delete character
iterator erase (iterator p);
Simple demonstration :

Delete the specified string
string& erase (size_t pos = 0, size_t len = npos);
Simple demonstration :

Among them len Default equal to npos,npos Is a static member variable :

When the second parameter is not given len When explicit value , It will be deleted from the beginning to the end of the string .

When given len When the value is greater than the length of the original string , and len Without assignment, the effect is the same , Delete until the end of the string .
swap--string Type exchange
string Class swap
void swap (string& str);What is exchanged is the pointer of two objects , The content pointed to by the pointer changes , High exchange efficiency :

Global function swap
Is the exchange of content , But because it is a custom type , It will involve deep copy , Low efficiency , Spending big :

c_str-- Return array pointer
Is equivalent to string Types become more familiar to us char* type :

Simple demonstration :

int main()
{
string s1("hello");
cout << typeid(s1).name() << endl;
cout << typeid(s1.c_str()).name() << endl;
return 0;
}find-- Specify location lookup
To find the character
size_t find (char c, size_t pos = 0) const;
Find the character at the specified position , Returns the subscript position of the matched character , Otherwise return to npos-- Shaping max
Find string
size_t find (const char* s, size_t pos = 0) const;Specify the starting position to find the matching substring , Return the subscript position at the beginning of the substring, otherwise return npos

From the subscript for 6 Start looking for , Match to string , Returns the subscript position of a string
rfind-- Search back and forth

and find Function the same , It's just that the starting direction is different , One before, one after , It's easy to understand
substr-- Take the string
string substr (size_t pos = 0, size_t len = npos) const;
pos Is the starting position ,len Is the length of the substring , Return this substring
For example :

If not found, it will return npos, It can be used as a judgment condition
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-- Get a row
Will not be affected by spaces :
When we use cin When entering a line of string , If there is a space in the middle , Will stop getting the string after the space :

You can use getline, usage :
istream& getline (istream& is, string& str);
string That's the end of the common usage of , Partners interested in other usages can consult documents and learn independently , Recommended sites :
cplusplus.com - C++ Resource networks
I hope to make progress with you !!!
边栏推荐
- Visual slam Lecture 3 -- Lie groups and Lie Algebras
- 集成底座方案演示说明
- 蓝桥杯单片机省赛第十届
- 《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
- 数据库文件逻辑结构形式指的是什么
- [数据库]JDBC
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
- Kotlin basic learning 16
- JS generate random numbers
- Review materials of project management PMP high frequency examination sites (8-1)
猜你喜欢

The first practical project of software tester: web side (video tutorial + document + use case library)

Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)

Influence of air resistance on the trajectory of table tennis
![[designmode] Prototype Pattern](/img/ee/c4e48c2ce8ff66f50f0bf13e5a0418.png)
[designmode] Prototype Pattern

Eight steps of agile development process

跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间

How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?

One of the future trends of SAP ui5: embrace typescript

The 9th Blue Bridge Cup single chip microcomputer provincial competition

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
Which of PMP and software has the highest gold content?
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
One of the future trends of SAP ui5: embrace typescript
Blue Bridge Cup single chip microcomputer sixth temperature recorder
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
接口调试工具模拟Post上传文件——ApiPost
蓝桥杯单片机省赛第五届
蓝桥杯单片机省赛第十二届第二场
Basic syntax of unity script (7) - member variables and instantiation
The 10th Blue Bridge Cup single chip microcomputer provincial competition
Fourier series
Unity脚本的基础语法(8)-协同程序与销毁方法
The 7th Blue Bridge Cup single chip microcomputer provincial competition
蓝桥杯单片机省赛第十二届第一场
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
Oracle viewing locked tables and unlocking
How about Ping An lifetime cancer insurance?
Custom classloader that breaks parental delegation