当前位置:网站首页>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 !!!
边栏推荐
- Xlwings drawing
- 【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
- VS2010插件NuGet
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
- Oracle 查看被锁的表和解锁
- The 11th Blue Bridge Cup single chip microcomputer provincial competition
- 蓝桥杯单片机省赛第六届
- Introduction to Robotics II. Forward kinematics, MDH method
- 接口调试工具模拟Post上传文件——ApiPost
- "Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
猜你喜欢

【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)

Learn more about materialapp and common attribute parsing in fluent

蓝桥杯单片机省赛第十二届第二场

Failed to upgrade schema, error: “file does not exist

【DesignMode】原型模式(prototype pattern)

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

"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network

MySQL之账号管理

Blue Bridge Cup single chip microcomputer sixth temperature recorder

In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
随机推荐
毕设-基于SSM电影院购票系统
Object oriented thinking
regular expression
跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间
Account management of MySQL
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions
[personal notes] PHP common functions - custom functions
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed
Which of PMP and software has the highest gold content?
Kotlin basic learning 13
Blue Bridge Cup SCM digital tube skills
[tips] use Matlab GUI to read files in dialog mode
Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU
UI (New ui:: MainWindow) troubleshooting
Failed to upgrade schema, error: “file does not exist
蓝桥杯单片机数码管技巧
Imageai installation
How about Ping An lifetime cancer insurance?
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network