[C++] Simple, practical and detailed explanation of string

The content of this video is to share knowledge about string. Before that, we need to introduce what STL is;

Table of contents

1. Brief introduction to STL

2. Brief introduction to string

3.string is easy to use

3.1.Definition of string

3.2. String concatenation

3.3.String traversal

3.3.1. Loop traversal

3.3.2. Iterator traversal

4. String function construction


1. Brief introduction to STL

STL is an important part of the C++ standard library. It is not only a reusable component library, but also a software framework including data structures and algorithms. In other words, it is a subset of the C++ standard library, which means that from now on, when we need to use the content of the data structure, we no longer need to define the structure ourselves like the C language, and it will be more convenient to use.

Of course, there are other components in the C++ standard library, among which STL is the most commonly used part, so we need to learn its use.

First understand the six major components of SLT

 The most important of the six components are algorithms and containers . Containers can actually be understood as data structures. The stacks, queues, trees, etc. we learned before are all called containers. We just changed the name of the data structure;

Algorithms are what we have learned before, such as sorting, inversion, exchange, etc., which can all be called algorithms. At present, what we need to focus on learning are container algorithms and iterators;

2. Brief introduction to string

Strictly speaking, string does not belong to STL, so string cannot be found in STL. String belongs to the standard library, so string cannot be found in the container .

But we can find string in the standard library 

 The reason is that string was born earlier than STL, but the usage of string is very similar to the containers we will learn in the future. It will be much easier for us to learn STL containers after learning string.

Next observe the description of string

The document says that string is an object implemented as a sequential array of characters, which can also be understood as managing a data table of characters;

In daily use, a lot of content needs to be represented by strings, such as ID number. If you use int to represent it, it will exceed the length, and some people's ID number ends in x.

Continue to observe the content included in string

 You can see that string also includes member functions (Member functions), iterators (Iterators), and content related to capacity (capacity). Regarding capacity, we can say that we have heard about it when learning data structures before. In learning C language, we often use structures such as arrays and sequence tables. When the capacity is not enough, we need to write our own functions to expand the capacity;

But in C++, the capacity content we use has self-expanding properties, that is, when the space is not enough, you can open your own space, which is very easy to use.

Next, use the code to taste the usage of Yipin string.

3.string is easy to use

3.1.Definition of string

In the above code, we have defined two strings, but s1 has not been initialized. The initialization method of s2 is copy construction. There are also many initialization methods as follows:

 It can be seen that we are using the first definition method without parameter construction, and the fourth definition method with parameters;

The same string also supports input and output

 

 Compared with C language, it solves the problem of crossing the boundary caused by input characters or applying for free space on demand. As long as our memory is large enough, strings of any length can be input and output.

3.2. String concatenation

String concatenation is very easy in C++

void test_string2()
{
	string s1("wddniubi ");
	string s2("hello world");
	string ret = s1 + s2;
	cout << ret << endl;
}
int main()
{
	test_string2();

	return 0;
}


We only need to add s1+s2 to concatenate strings, and keyboard input is also possible.

void test_string3()
{
	string s1;
	string s2;
	cin >> s1;
	cin >> s2;

	cout << s1 + s2 << endl;
}

int main()
{
	test_string3();

	return 0;
}

There are many ways to play, and it’s not much fun compared to strcat in C language.

strcat does not have enough space to expand on its own, and needs to find '\0' before appending, while string does not need to find the end first, saving time ;

3.3.String traversal

3.3.1. Loop traversal

void test_string4()
{
	string s1("hello world");

	//string遍历
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] ;
	}
	cout << endl;
	
}

In the above code, size is an internal function of capacity, which is used to calculate the size of the string, equivalent to strlen.

You can see the results of the traversal output;

At the same time, we can modify it by traversing

void test_string4()
{
	string s1("hello world");

	//string遍历
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] ;
	}
	cout << endl;
	string遍历修改      
	for (size_t i = 0; i < s1.size(); i++)
	{
		s1[i]++;
	}
	cout << s1;
}

 The above is the easiest traversal method for us to master, and there is another method called iterator

3.3.2. Iterator traversal

Go directly to the code and simply observe

void test_string5()
{
	string s1("hello world");
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it ;
		it++;
	}
	cout << endl;
}

 Here you need to use an iterator to define the variable it;

 First, we simply understand the iterator as a pointer

 There are three member variables here, among which _str points to the space where this string is stored;

Let's take a look at the functions in the iterator

 Like begin and the following are member functions in the iterator, where begin refers to the beginning of the string.

 The end points to the position of \0;

So we only need to specify the end conditions in the while loop, and the character array will traverse from the begin position of string s1 to the end position.

You can see that the output has been traversed through the iterator.

Similarly, in addition to reading data, iterators can also write data.

void test_string5()
{
	string s1("hello world");
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it ;
		it++;
	}
	cout << endl;

	it = s1.begin();
	while (it != s1.end())
	{
		*it='a';
		++it;
	}
	cout << endl;
	cout << s1 << endl;
}

We changed the contents of s1 to a, and the results are as follows

 There are also reverse iterators in iterators, such as the function rbegin, which is used in the same way as begin.

void test_string6()
{
	string s1("hello world");
	string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit ;
		++rit;
	}
	cout << endl;
}

 You can see that our hello world is reversed

 That's what reverse iterators look like.

 You can also use auto to complete the reverse iterator

void test_string6()
{
	string s1("hello world");
	auto rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit ;
		++rit;
	}
	cout << endl;
}

 auto will automatically deduce the return type of rbegin. The return type of rbegin is reverse_iterator.

4. String function construction

After talking about some commonly used functions in string, we might as well observe its function structure in depth, starting with the code observation

void test_string7()
{
	string s1("hello world");
	cout << s1 << endl;

	string s2(s1, 6, 5);
	cout << s2 << endl;

	string s3("helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
	string s4(s3, 3);
	cout << s4 << endl;
}

 

 You can see that string supports many copy construction methods, among which the third one has three parameters. The official analysis is as follows

 You can see that the third construction means to copy len characters of the string starting from the pos position, or until the end of the character when there is no len. You can observe the above test code. When we use s3 copy to construct s4 , the value of len is not given, the running result is as follows

 You can see that we directly get the end of string s4;

Similarly, we can also use iterator interval initialization;

void test_string7()
{

	string s5(5, 'a');
	
	string s6(s5.begin(), s5.end());
	cout << s6 << endl;
}

What this means is to initialize the first five data of s5 into a, and then initialize s6 with the beginning and end data of s5. The result is as follows

 You can see that the initialization was successful.

So there are many initialized versions of string, and the initialization of string is introduced here.

The purpose of learning string is to make it easier to learn containers in STL in the future. Many contents in STL are similar to the methods used in string. This is the purpose of learning string.

The above is what this film wants to share. If it is helpful to you, please support it three times. Thank you for reading.

Guess you like

Origin blog.csdn.net/wangduduniubi/article/details/132396820