当前位置:网站首页>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 !!!
边栏推荐
- In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
- Detailed explanation of ThreadLocal
- The 6th Blue Bridge Cup single chip microcomputer provincial competition
- 蓝桥杯单片机第六届温度记录器
- The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
- 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
- 数据库文件逻辑结构形式指的是什么
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
- The fourth provincial competition of Bluebridge cup single chip microcomputer
猜你喜欢

How should the team choose the feature branch development mode or trunk development mode?

【小技巧】使用matlab GUI以对话框模式读取文件

【DesignMode】建造者模式(Builder model)

Suggestions on settlement solution of u standard contract position explosion

Gradle foundation | customize the plug-in and upload it to jitpack

Flutter中深入了解MaterialApp,常用属性解析

一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)

蓝桥杯单片机第六届温度记录器

Basic syntax of unity script (6) - specific folder

The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
随机推荐
JS generate random numbers
[database]jdbc
Didi open source Delta: AI developers can easily train natural language models
Fourier series
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
SQL Yiwen get window function
[mv-3d] - multi view 3D target detection network
Jetpack之LiveData扩展MediatorLiveData
毕设-基于SSM电影院购票系统
Nacos 配置中心整体设计原理分析(持久化,集群,信息同步)
PY3, PIP appears when installing the library, warning: ignoring invalid distribution -ip
蓝桥杯单片机省赛第十一届第二场
Basic syntax of unity script (6) - specific folder
This article describes the step-by-step process of starting the NFT platform project
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
ThreadLocal详解
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
FFMpeg AVFrame 的概念.
蓝桥杯单片机省赛第六届