Learn how to use vector

Vector uses a class template and needs to be instantiated explicitly.

 

Single-parameter constructor supports implicit type conversion

 

Can vector<char> replace string?

The answer is of course no

1. String requires '\0' at the end, which is very compatible with C language.

Vector has no requirements and is not well compatible with C language.

2. The interface of string is richer than that of vector. String has many special interfaces, such as +=, comparing sizes (comparing sizes between vectors is of little significance)

 

Single-parameter constructor supports implicit type conversion

 

vector

d4a175411dff4e7aafea1b01ba91b420.png

Alloc: space allocator, memory pool (T improves memory application efficiency)

The following are template parameters

 

4dadad9c104a4423a22fd396fae83f1d.png

 

structure

c074e794beae4159aea7e070df7d7b64.png

 No parameter construction

n T (template) construction vector<int>s(10,1); vector<string>str(10,"***");

An iterator construction is written as a template here, so it is not limited to vector iterator initialization, and iterators of other containers can also be used.

copy construction

void test_vector3()
{
    //n个T初始化
	vector<int> v1(10, 1);
	vector<string> v2(10, "***");

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

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

	// 自己类型的迭代器
	vector<int> v3(v1.begin(), v1.end());
	for (auto e : v3)
	{
		cout << e << " ";
	}
	cout << endl;

    //其它类型的迭代器
	string str("hello world");
	vector<char> v4(str.begin(), str.end());
	for (auto e : v4)
	{
		cout << e << " ";
	}
	cout << endl;
    
    //指针
	int a[] = { 16,2,77,29 };
	vector<int> v5(a, a+4);
	for (auto e : v5)
	{
		cout << e << " ";
	}
	cout << endl;
}

b876d0dea10a4de98560725d1bc12873.png

 

Containers and Algorithms

Algorithm header file

#include<algorithm>

These intervals are generally closed on the left and open on the right.

Access data in a container via iterator

#include<algorithm>

int a[]={16,2,77,29};
vector<int>vint(a,a+4);

//升序
//默认less
sort(vint.begin(),vint.end());

for(auto e:vint)
cout<<e<<" ";
cout<<endl;

//降序
//greater
//greater<int>gt;
//sort(vint.begin(),vint.end(),gt);
//匿名对象
sort(vint.begin(),vint.end(),greater<int>());

for(auto e:vint)
cout<<e<<" ";
cout<<endl;

d7d18a5f42a14145932d89406268d8d3.png

 Reverse iterators can also sort descendingly. It is equivalent to arranging in reverse and traversing backwards.

 

reserve (open space, do not change size) and resize (open space + initialization, change size)

A common mistake in reserve

6dad73c04718487eaef6680533ddcf67.png

 This code will report an error and access out of bounds, because the reserve will not change the size.

99bfbf65cda3480587c9ae05b962067d.png

 

reserve should be used in conjunction with push_back to insert data

 

The solution is to use resize to open it

resize will be initialized and the size will also be increased.

void test_vector4()
{
//resize单独使用
	vector<int> v1;
	//cout << v1.max_size() << endl;
	//v1.reserve(10);
	v1.resize(10);
	for (size_t i = 0; i < 10; i++)
	{
		v1[i] = i;
	}

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

//reserve配合push_back使用
	vector<int> v2;
	v2.reserve(10);
	for (size_t i = 0; i < 10; i++)
	{
		v2.push_back(i);
	}
	for (auto e : v2)
	{
		cout << e << " ";
	}
	cout << endl;
}

 

at and []

When at crosses the boundary, an exception is thrown. Throwing an exception is used to record logs.

[] Out-of-bounds is an assertion. The assertion only takes effect in debug and does not work in release.
 

data is equivalent to c_str in string

 

push_back和pop_back

tail insertion, tail deletion

Head plug and head deletion are inefficient and do not provide corresponding interfaces.

 

erase and insert

Delete or insert at any position (can be used for head insertion and head deletion)

 

If we need to delete certain data, how do we find its location?

vector does not provide a search interface

However, a search interface is provided in the algorithm

The iterator that returns the corresponding position is found, and the next position that returns the last data is not found

a4c189d687104cbc897d295154c1efae.png

 

void test_vector5()
{
	int a[] = { 16,2,77,29,3,33,43,3,2,3,3,2 };
	vector<int> v1(a, a + sizeof(a)/sizeof(int));
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	// 头删
	v1.erase(v1.begin());
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	// 头插 
	v1.insert(v1.begin(), 100);
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	// 删除第3个数据
	v1.erase(v1.begin()+2);
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	// 删除3,但是不知道3在哪个位置,怎么办?
	//vector<int>::iterator pos = find(v1.begin(), v1.end(), 3);
	auto pos = find(v1.begin(), v1.end(), 3);
	if (pos != v1.end())
	{
		v1.erase(pos);
	}
}

 

So what do we need to delete all values ​​in the array?

We can delete one and find another, but when we start searching from the next deleted position, it involves the problem of iterator failure and the program crashes directly. Of course, if we start searching again, there will be no such problem, but the efficiency is too low.

void test_vector6()
{
	// 删除所有的3 -- 涉及迭代器失效!
	while(pos != v1.end())
	{
		v1.erase(pos);
		//pos = find(pos+1, v1.end(), 3);
		pos = find(v1.begin(), v1.end(), 3);
	}

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

	v1.assign(10, 1);
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;
}

 

assign

Clear the original data and reassign it

3f7ff48ed62749728d47d82cbc854d1e.png

 vector expansion logic

g++ uses the sgi version of STL

vs adopts pj version

81b9ecc06aa0449fb77d1fec62cfdc8c.png

 

Full sort

vector OJ question

phone number monogram

a18e2ea096ac4a089ea86c8c241c9e2e.png

 

 

Guess you like

Origin blog.csdn.net/2202_75625589/article/details/131609722
Recommended