当前位置:网站首页>First acquaintance with string+ simple usage (I)
First acquaintance with string+ simple usage (I)
2022-06-24 10:51:00 【I running】
Catalog
c++98 in string Common constructors
1. Construct an empty class object -- An empty string
2. Constant string initialization
3. Copy structure initialization
4. Specified length initialization .
5. Specify the characters and their number to initialize
6. Initializes the substring of an existing object
3. Iterator traverses backwards
1. Subscript access modification
2. In access object at Function to modify
string Reference and reference
string The capacity of the object
string What is it? :
string It's a class
string Class is basic_string An instance of the template class
Use char To instantiate basic_string Template class
The instantiated object is an array of characters that manages dynamic growth , With ‘\0’ ending
You need to include the header file when using string
#include <string>
c++98 in string Common constructors
1. Construct an empty class object -- An empty string
string + ( Object name )
string s1;
2. Constant string initialization
string + Object name (“ initialization ”)
string s2("hello csdn");
3. Copy structure initialization
string + Object name ( Object already exists )
string + Object name = Object already exists
string s3(s2); string s4 = s3;
The above three are the most commonly used initialization methods
4. Specified length initialization .
string + Object name (" Initialize content , Initialization length ")
string s5("asdfghjkl", 4);
5. Specify the characters and their number to initialize
string + Object name ( Number , character )
string s6(5,'c');
6. Initializes the substring of an existing object
string + Object name ( Object already exists , Specify starting position , Specify the length )
string s7(s2,3,5);
string Assignment
1. object 1= object 2
![]()
2. object = character string
3. object = character
string Object traversal
1.[ Subscript ] Traverse
void test3() { string s1("abcdefg"); //1.[ Subscript ] Traverse // It's used here size() function , The length of the string is returned , It doesn't contain \0 cout << s1.size() << endl; for (size_t i = 0;i < s1.size();i++) { // Here we use [], It's essentially function overloading : s1.operator[](i); cout << s1[i] << " "; } cout << endl; }
string Class is a custom type , use [] You need to find its operator overloaded function to call
2. Iterator forward traversal
void test4() { string s1("abcdefg"); //2. Iterator traversal --iterator Belong to string A built-in type in // This method can be used to solve some problems with large number of combinations string::iterator it = s1.begin(); //end() Is the next position of the end position while (it!=s1.end()) { cout << *it << " "; it++; } cout << endl; }We're just getting started with iterators , It can be understood as a pointer first ,begin() Point to the first character ,end() Points to the next... Of the last character , Generally point to '\0', The next step is to operate in pointer mode .
3. Iterator traverses backwards
void test7() { string s1("abcdefg"); //3. Iterator traverses backwards --reverse_iterator // This method can be used to solve some problems with large number of combinations string::reverse_iterator rit = s1.rbegin(); //end() Is the next position of the end position while (rit != s1.rend()) { cout << *rit << " "; rit++; } cout << endl; }The direction of traversal is opposite to that of the forward iterator , Pay attention to the grammatical form ,reverse_iterator、rbegin、rend
Although it is a reverse traversal , But still ++, No --
4. Range for
void test5() { //3. Range for Traverse string s1("hello csdn"); for (auto e:s1) { cout << e << " "; } cout << endl; }Create a variable e, according to s1 use auto Introduction e The type of , Ergodic time , automatically ++, Until I met '\0' end
Range for In the implementation of , Is actually replaced by an iterator
string Modification of
Now that you can traverse and access every character , The corresponding can also be used when accessing characters , Modify it
1. Subscript access modification
2. In access object at Function to modify
string Reference and reference
string Can be counted as a type , Parameters can be transferred
Here we directly transfer values and parameters , A formal parameter is a temporary copy of an argument
When the object is large , Consume large , therefore Formal parameter application reference passes parameter
Do not change the parameter content , Try to add const
add const Then you can't run through it , because begin and end There is a problem of permission amplification when returning
![]()
To solve the problem of making permissions the same , Then the iterator type should be const, The changes are as follows :
![]()
string The capacity of the object
string Type supports viewing the size and capacity of objects
Capacity
Call the object's capacity() function , You can view the capacity
But each dynamic development will increase the consumption , Is there any way to reduce the number of open space ?
reserve
Just change the size of the space , Yes size No impact
resize
![]()
string End insert of object
Insert characters push_back
Usage is as follows :
Insert string append
Two ways of using : Insert the string directly 、 Insert string Class object
direct +=
Support for characters and strings , Is the most widely used
边栏推荐
- Solve the timeout of Phoenix query of dbeaver SQL client connection
- [Qianfan 618 countdown!] IAAs operation and maintenance special preferential activities
- Shape change loader loads jsjs special effect code
- Image click enlargement and adaptive size in the applet rich text
- Web项目部署
- What is a compressed file? What are the advantages of different methods of compressing files?
- 26.删除有序数组的重复项
- Six states of threads
- Distribute proofs of manuscripts by scanning
- What characteristics should a good design website have?
猜你喜欢

charles抓包工具使用教程

Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars

Flink集群搭建以及企业级yarn集群搭建

【数据分析数据源】全国各省市行政区坐标(包含边界坐标点和中心坐标点)

Flink cluster construction and enterprise level yarn cluster construction

Stack Title: exclusive time of function

初识string+简单用法(一)

Shape change loader loads jsjs special effect code
![[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)

Process and multithreading
随机推荐
Canvas infinite scan JS special effect code
JMeter interface test tool foundation - sampler (II)
喜歡就去行動
【JS逆向分享】某个网站社区信息
Distributed transaction principle and solution
程序员大部分时间不是写代码,而是。。。
24. image mosaic operation
2022年智能机器人与系统国际研讨会(ISoIRS 2022)
A group of skeletons flying canvas animation JS special effect
Why use a firewall? What is the function of firewall?
Outils de capture de paquets
Spark提交参数--files的使用
Leetcode-929: unique email address
Wechat applet rich text picture width height adaptive method introduction (rich text)
Act as you like
Rising bubble canvas breaking animation JS special effect
SF Technology Smart logistics Campus Technology Challenge (June 19, 2022) [AK]
Smart energy: scenario application of intelligent security monitoring technology easycvr in the petroleum energy industry
2022 International Symposium on intelligent robots and systems (isoirs 2022)
初识string+简单用法(一)































