Simple operations and differences of vector, array and list

Posted by terryl on Mon, 07 Mar 2022 14:55:00 +0100

1, vector container

1.vector introduction

Vector container is a dynamic array provided in the form of class template. Similar to array, the elements of vector are also stored in continuous space, which supports subscript access to elements. Different from array, the size of vector container can be changed dynamically, and the storage space and allocation are completed by the container itself.

2. Define initialization

Header file < vector > to be imported

vector<int>  v1; //Define an empty vevtor of type int
vector<char> v2; //Define an empty vector of type char
vector<ClassName>; //Define an empty vector of xxx class 
vector<int> v3(10) // Define a vector containing 10 int elements
vector<int> v4(10,1) //Define a vector containing 10 int elements with an initial value of 1
string str[] = {"KB","LB","JB"};
vector<string> v1(str,str+2); //Define a vector that contains two string elements
vector<string> v2(v1);  //Vector can be assigned directly with object

3. Common functions of vector

	vector<int> v1;  //Define an empty vector of type int
	v1.begin();     // Returns a pointer to the vector header element
	v1.end();      // Returns a pointer to the tail element of the vector
	v1.rbegin();    // Points to the last element in the vector
	v1.rend();      //Points to the first element in the vector
	v1.push_back(1);   //Add to end 
	vector<int>::iterator it = v1.begin();//Iterator and points to the first element
	for (; it < v1.end(); it++)  //Traversing container elements
		cout << *it << endl;
	v1.pop_back();  //Delete last element
	v1.clear();     //Empty vector;        //Similar to array subscript access
	v1.size();       //Get how many elements are there in the vector
	v1.empty();      //Judge whether the vector is empty
	v1.assign(1, 3); //Modify the first element in the vector to 3

4. Traversal vector

Iterates through the entire vector

vector<int>::iterator it = v1.begin();//Iterator and points to the first element
for (; it < v1.end(); it++)  //Traversing container elements
		cout << *it << endl;

2, std::array container

1. Introduction to STD:: array

std::array was introduced in C++11. Compared with the built-in array, array is a safer and easier to use array type. Similar to the built-in array, the size of the array object is fixed. Therefore, array does not support adding and deleting elements and changing the size of containers. In addition to the traditional array supporting random access, high efficiency, fixed storage size and other characteristics, it also supports advanced functions such as iterator access, obtaining capacity, obtaining original pointer and so on.

2. Define initialization

Header file < array > to be imported

array<int , 5> a1; //Define an array with a length of 5, but no initialization value is random
array<int, 5> a2 = { 1,2,3,4,5 };  //Define an array with a length of 5 and initialize it
array<int, 5> a3(a2);   //array can also pass objects to vectors

3.array common functions

array<int, 5> a2 = { 1,2,3,4,5 };  //Define an array with a length of 5 and initialize it
a2.begin();           //First element in array
a2.end();            //Last element in array;           //Return to first address
a2.empty();          //Determine whether the array has elements;            //Similar array access

4. Traverse array

for (int i = 0; i < a2.size(); i++)
		cout <<;

3, list container

1.list introduction

The list container is a generalization of the two-way linked list. It is a linear sequence formed by connecting the elements stored in different locations through pointers. Each node of the two-way linked list has pointers to the front and back nodes respectively, so it is very convenient to traverse the container forward and backward in the list

2. Definition and initialization

Header file < list > to be imported

 list<int> list1;           //Create a list object of type int

 list<int> list2(5);       //Create a list with five int elements

 list<int>list3(3,2);  		//Create a list with 3 elements, and the element is 2

 list<int>list4(lst2);    //You can also assign values by passing objects

3.list common functions

		list1.back() 	//Returns the last element 
		list1.begin() 	//Returns an iterator that points to the first element 
		list1.clear() 	//Delete all elements 
		list1.empty() 	//Returns true if the list is empty 
		list1.end()		 //Returns the iterator at the end 
		list1.erase() 	//Delete an element 
		list1.front() 	//Returns the first element 
		list1.pop_back() //Delete last element 
		list1.pop_front() //Delete the first element 
		list1.push_back() //Add an element at the end of the list 
		list1.push_front() //Add an element to the head of the list 
		list1.rbegin() //Returns the inverse iterator pointing to the first element 
		list1.remove() //Delete element from list 
		list1.remove_if() //Delete elements according to specified conditions 
		list1.size() //Returns the number of elements in the list 
		list1.sort() //Sort list 
		list1.unique() //Delete duplicate elements in the list

4. Traverse the list

list<int>::iterator it = list1.begin();
for (it = list1.begin(); it != list1.end(); it++)
	cout << *it << endl;

4, Differences among vector, array and list

1. Difference between vector and array


(1) , vector and array memory are continuous
(2) , vector and array are similar to arrays and can be accessed using subscripts
(3) , vector and array have high security to avoid crossing the boundary


(1) The storage space of and vector is dynamic, but the storage space of array is fixed
(2) The data of and vector exist in the heap area, while the data of array exist in the stack area
(3) , vector can be inserted dynamically, but array cannot

2. Difference between vector and list

(1) The memory space of and vector is continuous, while the memory space of list is discontinuous
(2) vector can be accessed randomly, while list has low efficiency of random access
(3) If the memory of vector is not enough, it will actively open up new space, but the list does not need it, so the overhead of the list will be relatively small
(4) When inserting or deleting an element into a vector, you need to copy and move all the elements on the right of the element to be inserted; Therefore, it is more appropriate to use list when there are frequent insert and delete operations.

Topics: data structure