当前位置:网站首页>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 !!!
边栏推荐
- regular expression
- Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
- This article describes the step-by-step process of starting the NFT platform project
- Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
- 软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
- The 10th Blue Bridge Cup single chip microcomputer provincial competition
- Failed to upgrade schema, error: “file does not exist
- Blue Bridge Cup single chip microcomputer sixth temperature recorder
- 近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
- The 8th Blue Bridge Cup single chip microcomputer provincial competition
猜你喜欢

The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

NLog use

Large screen visualization from bronze to the advanced king, you only need a "component reuse"!

蓝桥杯单片机第四届省赛
![[tips] use Matlab GUI to read files in dialog mode](/img/51/6d6051836bfc9caa957d0275245bd3.png)
[tips] use Matlab GUI to read files in dialog mode

一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起

焱融看 | 混合雲時代下,如何制定多雲策略

【IBDFE】基于IBDFE的频域均衡matlab仿真

"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu

【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
随机推荐
Suggestions on settlement solution of u standard contract position explosion
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
Didi open source Delta: AI developers can easily train natural language models
Kotlin基础学习 17
In depth analysis of C language - variable error prone knowledge points # dry goods inventory #
[数据库]JDBC
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
The 10th Blue Bridge Cup single chip microcomputer provincial competition
焱融看 | 混合云时代下,如何制定多云策略
Kotlin basic learning 14
UI (New ui:: MainWindow) troubleshooting
5G时代全面到来,浅谈移动通信的前世今生
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
Learn more about materialapp and common attribute parsing in fluent
【DesignMode】建造者模式(Builder model)
Generate random numbers that obey normal distribution
Kotlin basic learning 16
《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
Network connection mode of QT