C + + relearning -- vector container

vector container

concept

  • Very similar to arrays
  • vector can be extended dynamically, but array cannot
  • Points to a contiguous piece of memory
  • Dynamic expansion is to find a new and larger space to store data, rather than adding a new space behind it

Iterator failure: when the vector finds a new and larger space, the previous iterator fails

structure

methodeffect
vector v;Using template implementation class implementation, default constructor
vector(v.begin() , v.end())Copy the elements in the v[begin(),end()) interval to itself (note that the front is closed and the back is open)
vector(n, elem)The constructor copies n elem s to itself
vector(const vector &vec)copy construction

assignment

It is relatively simple. Generally, both operator = and assign can be used

methodeffect
vector& operator=(const vector &vec)Overloaded equal sign operator
assign(beg,end);Assign the data copy in the [beg,end] interval to itself
assign(n,elem)Copy n elem s to itself

void printfVector(vector<int> &Obj)
{
	for (vector<int>::iterator it = Obj.begin(); it != Obj.end(); it++) 
	{
		cout << *it << " ";
	}
	cout << endl;
}

void func()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printfVector(v1);

	//Assignment method to 1 operator=
	vector<int> v2;
	v2 = v1;
	printfVector(v2);

	//assign assigns the data copy in the [beg,end] interval to itself
	vector<int> v3;
	v3.assign(v1.begin(), v1.end());
	printfVector(v3);

	//assign(n, elem) 	 Copy n elems to itself
	vector<int> v4;
	v4.assign(5,10);
	printfVector(v4);
}

Capacity and size

methodeffect
empty();Determine whether the container is empty
capacity();Capacity of container
size();Returns the number of elements in the container
resize(int num);Reassign the length of the container to num. if it becomes longer, the new position will be filled with the default value. If it becomes shorter, the part beyond the container will be deleted
resize(int num ,elem)Reassign the length of the container to num. if it becomes longer, fill the new position with elem. If it becomes shorter, the part beyond the container will be deleted

Note: resize changes size, not capacity

void printfVector(vector<int> &Obj)
{
	for (vector<int>::iterator it = Obj.begin(); it != Obj.end(); it++) 
	{
		cout << *it << " ";
	}
	cout << endl;
}

void func()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printfVector(v1);

	//empty(); 	 Determine whether the container is empty
	if (v1.empty()) 
	{
		cout << "vector Container is empty " << endl;
	}
	else
	{
		cout << "vector The capacity of the container is = " << v1.capacity() << endl;
		cout << "vector The size of the container is =  " << v1.size() << endl << endl;
		cout << "vector Smaller container" << endl;
		v1.resize(5);
		printfVector(v1);
		cout << endl;
		cout << "vector After expansion of the container" << endl;
		v1.resize(10, 6);
		printfVector(v1);
		cout << endl;
	}
}

Insert and delete

methodeffect
push_back(elem);Tail insert element
pop_back();Delete last element
insert(const_iterator pos,elem)The iterator inserts the element element element at the point
insert(const_iterator pos, int Count, elem)The iterator points to the position pos and inserts count elements elem
erase(const_iterator pos);Delete the element pointed to by the iterator
erase(const_iterator start, const_iterator end)Delete the elements of the iterator from start to end
clear();Delete all elements in the container

void printfVector(vector<int> &Obj)
{
	if (Obj.size() == 0)
	{
		cout << "vector The container is empty!";
	}
	for (vector<int>::iterator it = Obj.begin(); it != Obj.end(); it++) 
	{
		cout << *it << " ";
	}
	cout << endl;
}

void func()
{
	//Insert elements by tail interpolation
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printfVector(v1);

	//Delete last element
	v1.pop_back();
	printfVector(v1);

	//The iterator inserts the element element element at the point
	v1.insert(v1.begin(), 6);
	v1.insert(v1.begin(), 2, 50);
	printfVector(v1);

	//Delete the element pointed to by the iterator
	v1.erase(v1.begin());
	printfVector(v1);
	vector<int>::iterator it = v1.begin();
	it = it + 2;
	v1.erase(v1.begin(), it);	
	printfVector(v1);
	v1.clear();						//empty
	printfVector(v1);
}

data access

methodeffect
at(int idx);Returns the data indicated by the index idx
operator[];Returns the data indicated by the index idx
front();Returns the first data element in the container
back()Returns the last array element in the container


//Access the ith element with at()
void printfVector(vector<int> &Obj)
{
	if (Obj.size() == 0)
	{
		cout << "vector The container is empty!";
	}
	for (int i= 0;i<Obj.size();i++) 
	{
		cout << Obj.at(i) << " ";
	}
	cout << endl;
}

void func()
{
	//Access the ith element with []
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		//v1[i] = i;  Before that, because the capacity and size are 0, the statement belongs to cross-border access!
		v1.push_back(i);
	}
	printfVector(v1);

	cout << " The first element is  = " << v1.front() << endl;
	cout << "The last element is = " << v1.back() << endl;
}

Interchangeable container

methodeffect
swap(vec)Swap vec with its own elements

vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	cout << "v1 The container is:" << endl;
	printfVector(v1);

	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v2.push_back(i*10);
	}
	cout << "v2 The container is:" << endl;
	printfVector(v2);
	//Interchangeable container
	cout << "After container exchange" << endl;
	v2.swap(v1);
	cout << "v1 The container is:" << endl;
	printfVector(v1);
	cout << "v2 The container is:" << endl;
	printfVector(v2);

According to the characteristics of vector, the capacity of the container is > = the size of the container. If a container turns out to be very large, but the size is changed but the capacity is not changed after resizing, you can use the swap method to reduce the capacity

Principle: vector < int > (V1) swap(v1)

It can be decomposed into: vector < int > (V1) and swap(v1) two parts

Vector < int > (v1): indicates the creation of anonymous objects (for copy construction with v1)

. swap(v1): indicates that anonymous objects are exchanged with v1 to reduce the capacity of the container

Reserved space

Main function: reduce the number of vector development during dynamic capacity expansion

methodeffect
reserve(int len)The container reserves len element lengths, the reserved positions are not initialized, and the elements are inaccessible (change the capacity)

	int* p = NULL;	//Record the v1 first address each time
	int count = 0;	//Statistics of development times
	vector<int> v1;
	//If such a large space is known, it can be reserved first to avoid multiple development of Vector
	v1.reserve(100005);
	for (int i = 0; i < 100000; i++)
	{
		v1.push_back(i);
		if (p != &v1[0])
		{
			p = &v1[0];
			count++;
		}
	}

	cout << "Development times count = " << count << endl;

Keywords: C++

Added by lovelys on Mon, 07 Feb 2022 11:39:07 +0200