C++: Introduction of STL and string class

STL

What is STL

What is STL? STL is an important part of the C++ standard library, not only a reusable component library, but also a software framework including data structures and algorithms.

The six components of STL

To learn a new knowledge, it is necessary to know what is the content of this knowledge from a general perspective

The six major components are mainly as follows

insert image description here
STL is an important part of C++. If the process of simulating and implementing the previous data structure is regarded as a wheel, then STL is to use the wheels created by the predecessors to carry out more efficient
workstations

Then start to learn string. To learn new knowledge, you must have the ability to read documents. It is necessary to have the ability to read documents. This article and the future use the cplusplus website for text reading and knowledge learning.

string

insert image description here
The above is the content information intercepted from the website. First of all, the information we can get from it is that string is actually from typedef, which is from basic_string. Secondly, the standard string class provides this through an interface similar to the standard byte container. The support of class objects, but added features specially designed for manipulating single-byte strings, which will also be mentioned earlier, the string class is an object that provides sequential characters

  1. string is a string class that represents strings
  2. The interface of this class is basically the same as that of regular containers, and some regular operations specially used to manipulate strings are added.
  3. String is actually at the bottom: an alias of the basic_string template class, typedef basic_string<char, char_traits, allocator>string;
  4. cannot operate on sequences of multibyte or variable-length characters

Since string is a class, there must be member functions in the class:

Member functions in the string class

There are quite a lot of member functions in the string class object, here are some more commonly used ones

First the constructor:

insert image description here
Here is the usage of the constructor:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
	char arr[] = {
    
     "hello world" };
	string s1;   // 构造空的string对象
	string s2(arr); // 用cstring构造string类对象
	string s3(10, 'a'); // 构造包含10个a的string类对象
	string s4(s2);  // 拷贝构造
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << endl;
	return 0;
}

Followed by assignment overloading

This is the C++98 standard
insert image description here

On the basis of C++98, C++11 has made new changes and added new assignment methods, but overall, C++98 is still used more

insert image description here

way to access elements

insert image description here
It can be seen from this that the powerful function of operator overloading greatly increases the readability of the code. Take the following example:

int main()
{
    
    
	string s1 = "hello world";
	for (int i = 0; i < s1.size(); i++)
	{
    
    
		cout << s1[i] << " ";
	}
	cout << endl;
}

On the surface, this is the same as the traversal of strings in C language, but in fact it is not the same. Here, operator overloading is used to increase the readability of the code.

The following are operations related to capacity:
insert image description here

CRUD:

insert image description here

The convenience of string is also reflected in the addition, deletion, checking and modification. The above functions can realize the addition, deletion, checking and modification of the content in the string

iterator

Iterators are an important part of STL. In almost all containers, the usage of iterators is similar. Here we will focus on the introduction of iterators. The usage of other containers is similar to that here.

insert image description here
First of all, what is an iterator? What is the role of iterators?

From a functional point of view, an iterator can be understood as a pointer, and the pointer is used to perform multiple operations on the data stored in the string class. The function of the iterator can be member access to the members of the string class, thereby performing other operations such as reading and writing. have

Here first look at begin and end

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

The above is the usage of the iterator, and then look at how we traverse a string in C language?

int main()
{
    
    
	char s1[] = {
    
     "hello world" };
	char* ptr = s1;
	while (*ptr)
	{
    
    
		cout << *ptr << " ";
		ptr++;
	}
	cout << endl;
}

It can also be seen from this that in fact, iterators are pointers in terms of function.

Here you need to pay attention to how the definition of the iterator is defined

insert image description here

insert image description here

For different iterators, the definition method is different when defining

Let's look at rbegin and rend, which is different from the previous ones. There is an extra r in it. The meaning of this r is actually reverse, which means reverse, so rbegin means to read data in reverse.

int main()
{
    
    
	string s1 = "hello world";
	string::iterator it = s1.begin();
	string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
    
    
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
	while (it != s1.end())
	{
    
    
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

Guess you like

Origin blog.csdn.net/qq_73899585/article/details/132054226