当前位置:网站首页>First acquaintance with string+ simple usage (I)

First acquaintance with string+ simple usage (I)

2022-06-24 10:51:00 I running

Catalog

string What is it? :

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

string Assignment

1. object 1= object 2

2. object = character string

3. object = character

string Object traversal

1.[ Subscript ] Traverse

2. Iterator forward traversal

3. Iterator traverses backwards

4. Range for

string Modification of

1. Subscript access modification

2. In access object at Function to modify

string Reference and reference  

string The capacity of the object

Capacity

reserve

 resize

string End insert of object

Insert characters push_back

Insert string append 

direct += 


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

 

原网站

版权声明
本文为[I running]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240939317068.html