当前位置:网站首页>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
边栏推荐
- Process and multithreading
- 【IEEE出版】2022年服务机器人国际研讨会(IWoSR 2022)
- 机械臂速成小指南(三):机械臂的机械结构
- Several stacks of technology sharing: product managers' Online Official answers to several stacks of knowledge
- Leetcode-1051: height checker
- MYSQL_精讲数据库数据类型
- Smart energy: scenario application of intelligent security monitoring technology easycvr in the petroleum energy industry
- 【IEEE】自然语言处理与信息检索国际会议(ECNLPIR 2022)
- Leetcode-223: rectangular area
- Leetcode-1823: find the winner of the game
猜你喜欢

P5.js paper crane animation background JS special effect

Svg+js drag slider round progress bar

Spark提交参数--files的使用

SQL Server about like operator (including the problem of field data automatically filling in spaces)

Hill sorting graphic explanation + code implementation

Quick completion guide for manipulator (III): mechanical structure of manipulator

Process and multithreading

抓包工具charles實踐分享

283.移动零

Act as you like
随机推荐
Web project deployment
【IEEE出版】2022年自然语言处理与信息检索国际会议(ECNLPIR 2022)
Four methods of object merging and four methods of object merging in JS
Difference between package type and basic type
Tencent wetest platform will bring new benefits in 2021 with 618 special offers!
喜歡就去行動
[ei sharing] the 6th International Conference on ship, ocean and Maritime Engineering in 2022 (naome 2022)
线程运行原理
【数据分析数据源】全国各省市行政区坐标(包含边界坐标点和中心坐标点)
Practice sharing of packet capturing tool Charles
Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
JMeter interface test tool foundation - badboy tool
【IEEE出版】2022年服务机器人国际研讨会(IWoSR 2022)
Thread operation principle
Image click enlargement and adaptive size in the applet rich text
SF Technology Smart logistics Campus Technology Challenge (June 19, 2022) [AK]
System design: load balancing
Petit guide de construction rapide du bras mécanique (II): application du bras mécanique
今日睡眠质量记录76分
26.删除有序数组的重复项
































