C++STL vector (super detailed explanation)

Preface

Vector is an array, a dynamically growing sequential list.

What is the difference between it and string?
One is to manage arrays of any type, and the other is to manage character arrays.

Insert image description here
In order to improve the efficiency of memory application and release, its memory does not come directly from the computer, it comes from the memory pool.

The cost of learning vector is actually very low, because we have learned string before, and vector is actually a sequence table.

vector common interfaces

Because I have talked about STL before, I may not mention some commonly used ones, but they are actually the same as string.

Traversal mode

I have a question, how to traverse vector?
Insert image description here
Like string, we can use the following +[], iterator, and range for to access.

for (size_t i = 0; i < v.size(); ++i)
	{
    
    
		cout << v[i] << " ";
	}
	cout << endl;

	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
    
    
		cout << *it << " ";
		++it;
	}
	cout << endl;

	for (auto e : v)
	{
    
    
		cout << e << " ";
	}
	cout << endl;

This is the basic way to play our vector. In addition to reading the data of the vector, these three methods can also write the data of the vector.

copy construction

vector<int> copy(v);

Constructor

Insert image description here

second construct

vector<int> v1(10, 1);

Iterator range construction

vector<int> v2(v1.begin(), v1.end());

Note that all iterator ranges are closed on the left and open on the right.

Insert image description here
The iterator here can pass any type. It can not only pass vector, but also iterators of other containers, because it is a template.

string s1("hello world");
cout << s1 << " ";
cout << endl;
vector<char> v3(s1.begin() + 3, --s1.end());//很灵活
for (auto e : v3)
{
    
    
	cout << e << " ";
}
cout << endl;

Insert image description here

Iterator

It is uncomfortable to use iterators to traverse strings. Why use iterators?
The range for is defective. It can only be traversed directly, and it cannot be traversed from a certain position in the middle.
In fact, among our common containers, only string and vector are suitable for [], and the rest rely on iterators.

reverse iterator

//vector<int>::reverse_iterator rit = v.rbegin();
	auto rit = v.rbegin();
	while (rit != v.rend())
	{
    
    
		cout << *rit << " ";
		++rit;
	}
	cout << endl;

const iterator
const iterator is read-only and cannot be written. I will not demonstrate it in detail.

reserve

First, observe the expansion of vector.
Insert image description here
Insert image description here
VS really likes the 1.5 expansion.
g++ likes to double the size.

Knowing the amount of space and opening it in advance will improve efficiency.

resize

Resize is not used much in strings, but vectors like to use it very much.

vector<int> v1;
	v1.resize(10, 0);

insert and erase

There is still a difference between vector insert and string insert. Sting mainly uses subscripts. But vector uses iterators.
String is more for multiple data, vector is more for a single data.

It is not recommended to use insert and erase in vector. Anyone who has studied sequence tables knows it very well.

find

Vector does not provide search itself. If you want to search, use std.
Insert image description here

Why doesn't vector provide a find itself?
In fact, string designs its own find, which is mainly used to find substrings. There is no need for vector.

vector<int>::iterator pos = find(v.begin(), v.end(), 2);
if (pos != v.end())
{
    
    
	v.insert(pos, 20);
}

The last question is, with vector, can we use vector instead of string?
No, the difference between the two is quite big.

1. String has \0, but vector does not. String is more compatible with C.
2. It is meaningless to compare data in vector
. 3. It is very comfortable to use += for string. You can += one character or multiple characters. Vector cannot add multiple characters, and other types must be taken into account.
4.Vector provides find, which can find a single character and multiple characters.

Why design a separate string?
Because string has many separate and dedicated requirements, and vecotr cannot meet the various functional requirements of string.

17. Telephone number alphabet

Next, I will show you a question on leetcode:
the letter combination of a phone number.
Insert image description here
First of all, when you see this question, it is easy to see that it uses a deep traversal of a binary tree, but it also needs to consider other things.

Insert image description here
Since you want to use depth traversal, you definitely have to write another function. The first thing is to consider what parameters to pass. It doesn’t matter if you don’t consider them all. You can add them later when needed.

Insert image description here

Then we will finish the simple things first and outline the framework of deep traversal;
Insert image description here

Finally, we will consider combining data and combining it with depth traversal.
Insert image description here
Finally, the combination of range for and depth traversal is really exquisite.

Guess you like

Origin blog.csdn.net/weixin_68359117/article/details/134890306