[STL] Vector quick start

Table of contents

First, the template characteristics of vector

Second, the basic use of vector

1. Constructor

2. operator= assignment

3. vector - addition and deletion

A, end insertion && end deletion 

B,insert

C,  erase 

4. Access vector

Traverse the elements in the vector:

Method 1: array [] method || at method

Method 2: Iterator method

Syntactic sugar - for 

Three, application

1. Sort

2. vector + string


First, the template characteristics of vector

      A vector in STL is a dynamic array container that can store elements of any type. The template constructor of vector can implement different initialization methods through different parameters .

From the STL you will find:

    vector<int> s1;
    vector<double> s2;
    vector<char> s3;

    vector< string > s4 // data type is string class

Second, the basic use of vector

1. Constructor

The common ones are:

    vector<int> s1;         // 设置一个不包含任何元素的空对象
	vector<int> s2(3);      //设置3个初始值为默认值的vector对象
	vector<int> s3(3, 20);  // 三个初始值为20的vector对象
	// 以及还有迭代器的函数,这里留到讲解vector迭代器再讲
	vector<int> s4(s3);     // 拷贝构造

result:

2. operator= assignment

vector<int> s5 = s3;    // vector对象之间的赋值

3. vector - addition and deletion

There is no head insertion && head deletion in the vector, and there are insert (insert) && erase (delete)        accordingly

A, end insertion && end deletion 

 // 添加数据的值
    s1.push_back(10);  // 添加一个数据10
    s1.push_back(20);
    s1.push_back(30);
    s1.pop_back();  // 删除尾部元素
    s1.pop_back();

B,insert

 vector<int> s1; 
 s1.push_back(10);
 s1.push_back(10);
 s1.push_back(30);
 s1.push_back(21);
 vector<int>::iterator pos =  find(s1.begin(), s1.end(), 20);
	if (pos != s1.end()) //find()函数如果没有匹配的化会返回最后一个有效数据的下一个位置,
	 //                      没有判断则尾插,不符合判断插入的逻辑。
	{
		s1.insert(pos, 100); // pos为插入位置的迭代器,100则是插入值
	}

	for (auto i : s1)
	{
		cout << i << " ";
	}

// 结果是 10 10 30 21

 ( Note : the find() algorithm function is included in the algorithm header file)

C,  erase 

There are two types, one is to delete a single element, and the other is to delete a range, both of which are operated through iterators .

v1.erase(v1.begin() + 3); // delete the third element
v1.erase(v1.begin(), v1.end() - 1); // delete before v1.end() - 1 elements

4. Access vector

Common:

    s1[0];
	s1[2];
	s1.front();  // 返回头元素引用
	s1.back();   // 返回尾元素引用

Traverse the elements in the vector:

Method 1: array [] method || at method

    s1.push_back(10);
	s1.push_back(20);
	s1.push_back(30);

	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << endl; // s1[i]可换成s1.at(i)后者用的少
        // 都是返回数据的引用
	}

Method 2: Iterator method

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

Syntactic sugar - for 

At the same time, supporting iterators also supports syntactic sugar for, and its bottom layer will be replaced by iterators.

move:

for (auto i : s1)
	{
		cout << i << endl; 
	}

The access result is exactly the same as iterator.

Three, application

  We learned about the use of vector before, and after learning the string class, we will find that there is nothing particularly novel about vector. Here we will do some small knowledge point expansion.

1. Sort

The sorting algorithm function sort() in STL is used here 

As you can see, the sort function implements its functions through function templates, and its bottom layer uses the quicksort algorithm .

as follows:

    vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(21);
	v1.push_back(32);
	sort(v1.begin(), v1.end());
	for (auto e : v1)
	{
		cout << e << " ";
	}
	// 升序:10 20 21 30 32 40

The sort function defaults to ascending order, so how to change it to descending order ?

 Use the second overloaded function as follows:

    vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(21);
	v1.push_back(32);

	sort(v1.begin(), v1.end());
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	// 升序:10 20 21 30 32 40
    
    // 库函数中有两个仿函数
	less<int> ls;  // 默认的升序就是他
	greater<int> gt; // 降序
	sort(v1.begin(), v1.end(), gt);
	for (auto e : v1)
	{
		cout << e << " ";
	}
    //降序:40 32 30 21 20 10

Of course, there is also such a simple way of writing on the Internet:

  sort(v1.begin(), v1.end(), greater<int>());

And anonymous objects are used here (less and greater are classes).

2. vector + string

    We know that vector is generic programming, so what happens when the data type is a custom type? Such as: what kind of difference will there be in the string class?

 code show as below:

    vector<string> strv;
	string s1 = "张三";
	strv.push_back(s1);  // 调用拷贝构造
	strv.push_back(string("张三"));  // 使用了匿名对象,具有临时对象的性质(const string)
	// 本质上string使用了拷贝构造,操作还是比较繁琐。
	strv.push_back("张三");  // 大部分时候是这样一种写法,使用了string的构造

Ok, let's use the range for to traverse the output, please see the following code, is there any unreasonable place?

int main()
{
	vector<string> strv;
	string s1 = "张三";
	strv.push_back(s1);  // 调用拷贝构造
	strv.push_back(string("张三"));  // 使用了匿名对象,具有临时对象的性质(const string)
	// 本质上string使用了拷贝构造,操作还是比较繁琐。
	strv.push_back("张三");  // 大部分时候是这样一种写法,使用了string的构造

	for (auto str : strv)
	{
		cout << str << endl;
	}
	return 0;
}

Parsing: should be auto& str : strv . We know that the data in the vector container this time is of the string class, and the function of the range for is to assign the data in strv to str, and the string class to the string class. Isn’t this a copy construction ? Then each assignment is a deep copy, which is a big waste of performance , so it should be auto& str : strv (if it is more rigorous: the data cannot be modified, and the front is modified with const) 

Here is an algorithm question using vector: 118. Yang Hui Triangle

 

Supplement: double brackets for vector

 

 Essentially, the subscripted double brackets are calling the function twice .

The above common usage of vector, if you want to continue to understand other usages, you need to check the document: Reference - C++ Reference (cplusplus.com) 

epilogue

This section is over here, thank you friends for browsing, if you have any suggestions, welcome to comment in the comment area; if you bring some gains to your friends, please leave your likes, your likes and concerns will become bloggers The driving force of the master's creation.

Guess you like

Origin blog.csdn.net/qq_72112924/article/details/131185846