[C++ basic knowledge] vector

vector introduction

  1. A vector is a sequence container that represents a variable-size array.
  2. Just like arrays, vectors also use contiguous storage space to store elements. This means that you can use subscripts to access the elements of a vector, which is as efficient as an array. But unlike an array, its size can be changed dynamically, and its size will be automatically processed by the container.
  3. Essentially, vector uses a dynamically allocated array to store its elements. When new elements are inserted, this array needs to be re-allocated in order to increase storage space. The method is to allocate a new array, and then move all the elements to this array. In terms of time, this is a relatively expensive task, because every time a new element is added to the container, the vector does not reallocate its size every time.
  4. Vector allocation strategy: vector will allocate some additional space to accommodate possible growth, because the storage space is larger than the actual storage space needed. Different libraries adopt different strategies to balance the use and reallocation of space. But in any case, the redistribution should be a logarithmic increase of the interval size, so that when an element is inserted at the end, it is done in constant time complexity.
  5. Therefore, vector occupies more storage space, in order to obtain the ability to manage storage space, and dynamically grow in an effective way.
  6. Compared with other dynamic sequence containers (deques, lists and forward_lists), vector is more efficient when accessing elements, and adding and deleting elements at the end is relatively efficient. For other delete and insert operations that are not at the end, the efficiency is lower. Better than unified iterators and references for lists and forward_lists

Common interface

structure

explicit vector (const allocator_type& alloc = allocator_type());
explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());
vector (const vector& x);
template <class InputIterator>

vector (InputIterator first, InputIterator last,const allocator_type& alloc = allocator_type());

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

void test()
{
    
    
	class A {
    
    

	};

	vector<int>v1; 
	vector<char>v2;
	vector<A>v3;
	vector<A> v4 (10);
	A a;
	vector<A> v5(20, a);

	vector<int>v7(10);
	vector<char>v8(10);
	vector<int *>v9(10);

	char arr[] = "123456";
	vector<char>v10(arr, arr + sizeof(arr) / sizeof(arr[0]));

}

int main()
{
    
    
	test();
	return 0;
}

Insert picture description here

The container v7 storing integer data is initialized to all 0;
the container v8 storing character data is initialized to all'\0';
the container v9 storing integer pointer is initialized to all NULL;

Insert picture description here

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


template<class T>
void Print(const vector<T>&vec)
{
    
    
	vector<T>::const_iterator it = vec.begin();
	while (it != vec.end())
	{
    
    
		cout << *it << " ";
		++it;
	}
	cout << endl;
}
int main()
{
    
    
	char arr[] = "123456";
	vector<char>v10(arr, arr + sizeof(arr) / sizeof(arr[0]));
	Print(v10);
	return 0;
}

Insert picture description here

Iterator

Use of iterator Description
Forward iterator: begin + end Get the iterator/const_iterator of the first data position, get the iterator/const_iterator of the next position of the last data
Reverse iterator: rbegin + rend Get the reverse_iterator of the last data position, get the reverse_iterator of the first data position
	char arr[] = "123456";
	vector<char>v10(arr, arr + sizeof(arr) / sizeof(arr[0]));


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

Insert picture description here

Note: The
iterator can also be written here to change the value in the container. The
reverse iterator is similar to the forward iterator, but the direction is opposite (++,--operations are opposite)

Look at an example:

#define _CRT_SECURE_NO_WARNINGS 1

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


template<class T>
void Print(const vector<T>&vec)
{
    
    
	vector<T>::const_iterator it = vec.begin();
	while (it != vec.end())
	{
    
    
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

void test()
{
    
    
	class A {
    
    

	};

	vector<int>v1; 
	vector<char>v2;
	vector<A>v3;
	vector<A> v4 (10);
	A a;
	vector<A> v5(20, a);

	vector<int>v7(10);
	vector<char>v8(10);
	vector<int *>v9(10);

	char arr[] = "123456";
	vector<char>v10(arr, arr + sizeof(arr) / sizeof(arr[0]));


	Print(v7);
	Print(v8);
	Print(v9);
	Print(v10);

}

int main()
{
    
    

	test();
	return 0;
}

Output result:
Insert picture description here

v8 is all'\0' so invisible

Assignment

void test()
{
    
    
	vector<int>v(3, 2);
	Print(v);
	//赋值
	v.assign(5,1);
	Print(v);

	vector<int>v2(6, 2);
	v.assign(v2.begin(),v2.end());
	Print(v);

	int arr[] = {
    
     1,2,3,4,5,6,7 };
	v.assign(arr,arr+sizeof(arr)/sizeof(arr[0]));
	Print(v);

}

Insert picture description here

Add, delete, check and modify

vector add, delete, check and change Description
push_back Tail plug
pop_back Tail delete
find Find
insert insert
erase Delete the data at the specified location
swap Exchange the data space of two vectors
operator[] Access like an array (exception will be reported after crossing the boundary)

vector iterator failure problem

The main function of the iterator is to allow the algorithm to not care about the underlying data structure, which is actually a pointer or encapsulates the pointer. For example, the iterator of the vector is the original pointer T*. Therefore, the iterator fails, the actual space pointed to by the corresponding pointer at the bottom of the iterator is destroyed, and the use of a free space will cause the program to crash (that is, if you continue to use the invalidated iterator, the program may crash ).

  1. Operations that will cause changes in the underlying space may be the iterator failure, such as: resize, reserve, insert, assign, push_back, etc.
  2. Delete operation of the specified position element-erase

Solution : Iterator is invalid. Solution: Before using, just re-assign the iterator.

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/115267058