C++ STL learning notes - vector

preface

Vector is a vector type. Like a container, it can store any type of dynamic array and increase and reduce data. Unlike arrays, which can only be static space, the implementation of vector uses pointers.

vector is part of the C + + standard template library. It is a multifunctional template library and function class that can operate a variety of data structures and algorithms.

It should be declared in the header file before use, that is: #include < vector >

Then add a definition in front of the code, that is: vector < type > V;

Although vector is a one-dimensional dynamic array, we can use the idea similar to array to open two dimensions for it

Shape similarity:

vector< vector<int> > v;

be careful:

The so-called dynamic size increase is not to connect a new space after the original space (because there is no configurable space after the original space), but a larger memory space, then copy the original data to the new space and release the original space. Therefore, once any operation on the vector causes a space reconfiguration, all iterators pointing to the original vector will fail.

Basic Usage

Most commonly used are as follows:

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

int a[]={1,2,3,4,5};
vector<int> v;
//General create vector
vector<int> v1(a,a+sizeof(a)/sizeof(int));
//Copy the elements in the a[begin(),end()) interval to itself
vector<int> v2(10,1);
//Construct a container with n elements (essentially copy n elements to itself) 

int main(){
    cout<<"v v1 v2 The number of elements in each of these three containers:"<<endl; 
	cout<<v.size()<<" "<<v1.size()<<" "<<v2.size()<<endl;
	//size() returns the number of elements in the container
	if(v.empty()) cout<<"v.empty()==1 v There are no elements in it"<<endl;
	//empty() determines whether the v container is empty. It returns 1 if it is empty and 0 if it is not. That is, it is a bool type function 
	v.push_back(a[0]);
	cout<<"v.push_back(a[0]) here v The container is inserted a[0]"<<endl; 
	//push_ Insert an element at the end of back() 
	if(!v.empty()) cout<<"!v.empty()==1 v There are elements in it"<<endl;
	v.insert(v.end(),a[1]);
	cout<<"use v.insert() Insert a at the end of the container a[1]"<<endl; 
	/*
	insert() Represents that an element has been inserted at a location in the v container 
	There are three parameters in insert() (pos,number,element)
	That is, number element s are inserted in the pos position 
	*/ 
	cout<<"here v The number of elements in the container is:"<<v.size()<<endl;
	cout<<"v The first number of containers is: "<<v.front()<<endl<<"v The last number of containers is: "<<v.back()<<endl;
	//front() returns the first data element in the container, and back() returns the last data element in the container
	v.resize(4);
	//resize() is a function to reassign the container length. It has two parameters. The first parameter is the reassigned length, the second is the element to be filled, and the second is 0 by default if it is not filled 
	cout<<"Let's put it again v The container is reassigned to a length of 4"<<endl; 
	cout<<"here v The length of the container is: "<<v.size()<<endl;
	cout<<"Let's reassign the length v Container output once"<<endl; 
	vector<int>::iterator i;
	//This is using a vector iterator to access elements similar to pointers 
	cout<<"v The containers are from beginning to end: "; 
	for(i=v.begin();i!=v.end();i++) cout<<*i<<" ";
	//It can be seen that when the length is reassigned, the extra position is filled with the default value, which is zero
	cout<<"Now specify to delete the first element"<<endl; 
	v.erase(v.begin());
	//erase() has two parameters, posl and posr. If there is only posl, only the elements at that position will be deleted, while if there are both posl and posr, the interval elements will be deleted 
	cout<<"here v The length of the container is: "<<v.size()<<endl;
	cout<<"v The containers are from beginning to end: "; 
	for(i=v.begin();i!=v.end();i++) cout<<*i<<" ";
	cout<<endl; 
	v.resize(5,5);
	cout<<"Let's put it now v The container is reassigned to a length of 5 and the filled number is changed to 5"<<endl;
	cout<<"v The containers are from beginning to end: ";
	for(i=v.begin();i!=v.end();i++) cout<<*i<<" ";
	cout<<endl; 
	cout<<"Now delete the last element, but with pop_back"<<endl;
	v.pop_back();
	//pop_back() deletes the last element 
	cout<<"v The containers are from beginning to end: ";
	for(i=v.begin();i!=v.end();i++) cout<<*i<<" ";
	cout<<endl; 
    system("pause");
}

Other usage:

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

vector<int> v,v1;
int a[5]={1,2,3,4,5};

int main(){
	v.assign(a,a+sizeof(a)/sizeof(int));
	// assign() copies the data elements in the a array to the container 
	v1.assign(3,1);
	//assign() copies n elements into the container 
	vector<int>::iterator it;
	cout<<"v The elements contained in the container are: ";
	for(it=v.begin();it!=v.end();it++) cout<<*it<<" ";
	cout<<endl;
	cout<<"v1 The elements contained in the container are: "; 
	for(it=v1.begin();it!=v1.end();it++) cout<<*it<<" "; 
	cout<<endl; 
	v.swap(v1);
	//swap() swaps the elements of the containers V and v1 
	cout<<"use v.swap(v1)"<<endl;
	cout<<"v The elements contained in the container are: ";
	for(it=v.begin();it!=v.end();it++) cout<<*it<<" ";
	cout<<endl;
	cout<<"v1 The elements contained in the container are: "; 
	for(it=v1.begin();it!=v1.end();it++) cout<<*it<<" "; 
	cout<<endl; 
	v.clear();
	v1.clear(); 
	//clear() deletes all elements in the container 
	cout<<"here v and v1 The number of elements contained in is: ";
	cout<<v.size()<<" "<<v1.size()<<endl; 
	return 0;
} 

The same applies to sort and reverse in algorithm, such as

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

vector<int> v,v1;
int a[5]={1,3,2,5,4};

int main(){
	v.assign(a,a+sizeof(a)/sizeof(int));
	cout<<"v The elements contained in the container are: ";
	vector<int>::iterator it;
	for(it=v.begin();it!=v.end();it++) cout<<*it<<" ";
	cout<<endl;
	reverse(v.begin(),v.end());
	cout<<"v The elements contained in the container are: ";
	for(it=v.begin();it!=v.end();it++) cout<<*it<<" ";
	cout<<endl;
	sort(v.begin(),v.end());
	cout<<"v The elements contained in the container are: ";
	for(it=v.begin();it!=v.end();it++) cout<<*it<<" ";
	cout<<endl;
	return 0;
} 

Of course, there are some operations besides these, but I haven't used them, so I won't repeat them here.

We can summarize:

Borrowed here Master Shen Xiao's detailed explanation of [C/C + +] STL

vector constructor

vector<T> v; //Using template implementation class implementation, default constructor
vector(v.begin(), v.end());//Copy the elements in the v[begin(), end()) interval to itself.
vector(n, elem);//The constructor copies n elem s to itself.
vector(const vector &vec);//Copy constructor.

//Using the second constructor, we can
int arr[] = {2,3,4,1,9};
vector<int> v1(arr, arr + sizeof(arr) / sizeof(int)); 

vector common assignment operations

assign(beg, end);//Assign the data copy in the [beg, end) interval to itself.
assign(n, elem);//Assign n elem copies to itself.
vector& operator=(const vector  &vec);//Overloaded equal sign operator
swap(vec);// Swap vec with its own elements.

vector size operation

size();//Returns the number of elements in the container
empty();//Determine whether the container is empty
resize(int num);//Reassign the length of the container to num. if the container becomes longer, fill the new location with the default value. If the container becomes shorter, the element whose end exceeds the length of the container is deleted.
resize(int num, elem);//Reassign the length of the container to num. if the container becomes longer, fill the new position with elem value. If the container becomes shorter, the element whose end exceeds the container length > degrees is deleted.
capacity();//Capacity of container
reserve(int len);//The container reserves len element lengths. The reserved positions are not initialized and the elements are inaccessible.

vector data access operation

at(int idx); //Returns the data indicated by the index idx. If the IDX is out of bounds, an out is thrown_ of_ Range exception.
operator[];//Returns the data indicated by the index idx. If it exceeds the limit, the operation will directly report an error
front();//Returns the first data element in the container
back();//Returns the last data element in the container

vector insert and delete operations

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

Use of vector storage elements

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

vector<int> v,v1;
int a[5]={1,3,2,5,4};

int main(){
	v.assign(a,a+sizeof(a)/sizeof(int));
	//Method 1: use iterators to access elements
	vector<int>::iterator it;
	for(it=v.begin();it!=v.end();it++) cout<<*it<<" ";
	cout<<endl;
	//Method 2: use at() function
	for(int i=0;i<v.size();i++) cout<<v.at(i)<<" ";
	cout<<endl;
	for(int i=0;i<v.size();i++) cout<<v[i]<<" ";
	cout<<endl; 
	return 0;
} 

Storing adjacency table with vector

This is a very easy method to use

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

struct edge{
	int to;
	int val;
}e;

vector<edge> v[10100];
int n,m,x,y,w;

int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>x>>e.to>>e.val;
		v[x].push_back(e);
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<v[i].size();j++){
			e=v[i][j];
			printf("from %d to %d, the cost is %d\n",i,e.to,e.val);
		}
	}
}

Graph theory learning notes - linked list and adjacency list

Sample input:

5 5
1 2 1
2 3 2
3 4 3
4 5 4
1 5 5

Sample output:

from 1 to 2, the cost is 1
from 1 to 5, the cost is 5
from 2 to 3, the cost is 2
from 3 to 4, the cost is 3
from 4 to 5, the cost is 4

Keywords: C++ Back-end

Added by findshorty on Sun, 02 Jan 2022 03:02:51 +0200