STL_vector container

1. Introduction to Vector Container

A vector is a container that places elements in a dynamic array for management.

Vector can access elements randomly (support index value direct access, use [] operator or at() method).

Adding or removing elements from the tail of a vector is very fast, but inserting or removing elements in the middle or head is time-consuming.

Second, the default structure of the vector object

vector is implemented by template class, the default structure of vector object: vector<T> vecT;

vector<int> vecInt;     //一个存放int的vector容器。

vector<float> vecFloat;    //一个存放float的vector容器。

vector<string> vecString;   //一个存放string的vector容器。

...                  //尖括号内还可以设置指针类型或自定义类型。

Class CA{
    
    };

vector<CA*> vecpCA;      //用于存放CA对象的指针的vector容器。

vector<CA> vecCA;       //用于存放CA对象的vector容器。由于容器元素的存放是按值复制的方式进行的,所以此时CA必须提供CA的拷贝构造函数,以保证CA对象间拷贝正常。

Three, the parameter structure of the vector object

vector(beg,end); //The constructor copies the elements in the range [beg, end) to itself. Note that the interval is left closed and right opened.

vector(n,elem); //The constructor copies n elems to itself.

vector(const vector &vec); //copy constructor

void printVector(vector<int>& v) {
    
    
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}
	vector<int> vl;//默认构造

	int arr[] = {
    
     10, 20, 30, 40 };
	vector<int> v2(arr, arr + sizeof(arr) / sizeof(int));
	vector<int> v3(v2.begin(), v2.begin()+3);
	vector<int> v4(v3);
	vector<int> v5(4,5);

	printVector(v2);
	printVector(v3);
	printVector(v4);
	printVector(v5);
/*
结果:
10 20 30 40
10 20 30
10 20 30
5 5 5 5
*/

Four, the assignment of vector

vector.assign(beg,end); //Assign a copy of the data in the interval [beg, end) to itself. Note that the interval is left closed and right opened.

vector.assign(n,elem); //Assign n copies of elem to itself.

vector& operator=(const vector &vec); //overload the equal sign operator

vector.swap(vec); // Swap vec with its own elements.

	int arr[] = {
    
     10, 20, 30, 40 };
	vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));//默认构造

	//成员方法
	vector<int> v2;
	v2.assign(v1.begin(), v1.end());

	//重载=
	vector<int> v3;
	v3 = v2;

	int arr1[] = {
    
     100, 200, 300, 400 };
	vector<int> v4(arr1, arr1 + sizeof(arr) / sizeof(int));//默认构造

	printVector(v1);
	printVector(v2);
	printVector(v3);
	printVector(v4);

	cout << "------------------" << endl;

	v4.swap(v1);
	printVector(v1);
	printVector(v2);
	printVector(v3);
	printVector(v4);
/*
结果:
10 20 30 40
10 20 30 40
10 20 30 40
100 200 300 400
------------------
100 200 300 400
10 20 30 40
10 20 30 40
10 20 30 40
*/

Five, the size of the vector

vector.size(); //Returns the number of elements in the container

vector.empty(); //Determine whether the container is empty

vector.resize(num); //Re-specify the length of the container as num. If the container becomes longer, fill the new position with the default value. If the container becomes shorter, the elements at the end that exceed the length of the container are deleted.

vector.resize(num, elem); //Re-specify the length of the container as num. If the container becomes longer, fill the new position with the elem value. If the container becomes shorter, the elements at the end that exceed the length of the container are deleted.

capacity(); //The capacity of the container

reserve(int len);//The container reserves len element length, the reserved position is not initialized, and the element is not accessible.

	int arr1[] = {
    
     100, 200, 300, 400 };
	vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));//默认构造

	cout << "size:" << v4.size() << endl;
	if (v4.empty()) {
    
    
		cout << "空!" << endl;
	}
	else {
    
    
		cout << "不空!" << endl;
	}

	printVector(v4);
	v4.resize(2);
	printVector(v4);
	v4.resize(6);
	printVector(v4);
	v4.resize(8, 1);
	printVector(v4);

	for (int i = 0; i < 10000; i++) {
    
    
		v4.push_back(i);
	}
	cout << "size:" << v4.size() << endl;  
	cout << "容量:" << v4.capacity() << endl;//容量不一定等于size
/*
结果:
size:4
不空!
100 200 300 400
100 200
100 200 0 0 0 0
100 200 0 0 0 0 1 1
size:10008
容量:12138
*/

Six, add and remove operations at the end of vector

vector.push_back(); //Add an element to the end of the container

vector.pop_back();; //Remove the last element in the container

	int arr1[] = {
    
     100, 200, 300, 400 };
	vector<int> v1(arr1, arr1 + sizeof(arr1) / sizeof(int));//默认构造

	v1.push_back(500);
	v1.push_back(600);
	v1.push_back(700);
	v1.pop_back();
	printVector(v1);
/*
结果:
100 200 300 400 500 600100 200 300 400 500 600
*/

Seven, vector data access

vector.at(int idx); //Return the data pointed to by index idx, if idx is out of range, throw out_of_range exception.

vector[int idx];//returns the data pointed to by the index idx, when the boundary is exceeded, an error will be reported directly

vector.front();//returns the first data element in the container

vector.back();//Return to the last data element in the container

	int arr1[] = {
    
     100, 200, 300, 400 };
	vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));//默认构造

	//区别: at抛异常 []不抛异常
	for (int i = 0; i < v4.size(); i++) {
    
    
		cout << v4[i] << " ";
	}
	cout << endl;

	for (int i = 0; i < v4.size(); i++) {
    
    
		cout << v4.at(i) << " ";
	}
	cout << endl;

	cout << "front:" << v4.front() << endl;
	cout << "back:" << v4.back() << endl;
/*
结果:
100 200 300 400
100 200 300 400
front:100
back:400
*/

8. Insert and delete vector

vector.insert(pos,elem); //Insert a copy of the element element at pos and return the position of the new data.

vector.insert(pos,n,elem); //Insert n elem data at position pos, no return value.

vector.insert(pos,beg,end); //Insert the data in the [beg,end) interval at position pos, no return value

vector.clear(); //Remove all data in the container

vector.erase(beg,end); //Delete the data in the [beg,end) interval and return the position of the next data.

vector.erase(pos); //Delete the data at the pos position and return the position of the next data.

	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	//头插法
	v.insert(v.begin(), 30);
	v.insert(v.end(), 40);

	v.insert(v.begin() + 2, 100); //vector支持随机访问

	//支持数组下标,一般都支持随机访问
	//迭代器可以直接+2 +3 -2 -5操作
	printVector(v);

	//删除
	v.erase(v.begin());
	printVector(v);
	v.erase(v.begin() + 1, v.end());
	printVector(v);
	v.clear();
	cout << "size:" << v.size() << endl;
/*
结果:
30 10 100 20 40
10 100 20 40
10
size:0
*/

Nine, use swap cleverly to shrink memory space

vector<T>(x).swap(x); //Among them, x refers to the container to be operated currently, and T is the type of element stored in the container.

	//vector添加元素 他会自动增长 你删除元素时候,不会自动减少

	vector<int> v;
	for (int i = 0; i < 100000; i++) {
    
    
		v.push_back(i);
	}

	cout << "size:" << v.size() << endl;
	cout << "capacity:" << v.capacity() << endl;

	v.resize(10);
	cout << "--------------" << endl;
	cout << "size:" << v.size() << endl;
	cout << "capacity:" << v.capacity() << endl;

	//收缩空间
	vector<int>(v).swap(v);

	cout << "--------------" << endl;
	cout << "size:" << v.size() << endl;
	cout << "capacity:" << v.capacity() << endl;
/*
结果:
size:100000
capacity:138255
--------------
size:10
capacity:138255
--------------
size:10
capacity:10
*/

10. Reserve reserved space

The reserve of vector increases the capacity of vector, but its size has not changed! Resize changes the capacity of the vector and also increases its size!

The reasons are as follows:

  • Reserve is the reserved space of the container, but the element object is not actually created in the space, so you cannot refer to the element in the container before adding a new object. When a new element is added, push_back()/insert() function should be called.
  • Resize is to change the size of the container and create the object. Therefore, after calling this function, you can refer to the object in the container. Therefore, when adding a new element, use the operator[] operator or use an iterator to refer to the element Object. At this time, the push_back() function is called again, which is added behind this new space.
	int num = 0;
	int* address = NULL;

	vector<int> v;
	v.reserve(100000);
	for (int i = 0; i < 100000; i++) {
    
    
		v.push_back(i);
		if (address != &(v[0])) {
    
    
			address = &(v[0]);
			num++;
		}
	}

	cout << "num:" << num << endl;//申请num次空间
/*
结果:
num:1
*/

If a vector uses the default capacity, then during the push_back operation, the space will be dynamically allocated automatically according to the number of elements added, increasing by 2^n; if the vector is declared, the capacity(size_type n) is used explicitly to specify The capacity of the vector, then in the process of push_back (the number of elements does not exceed n), the vector will not automatically allocate space, improving the efficiency of the program.

Guess you like

Origin blog.csdn.net/weixin_45341339/article/details/113059128