The usage of C++ vector

Keywords: less Programming

Vector is a vector type, which can hold many types of data, such as several integers, so it is called a container. Vector is an important member of C++ STL. When using it, you need to include the header file:


1. Initialization of vector: there are five ways, as shown in the following example:

    (1) vector<int> a(10); //The vector of 10 integer elements (the name of element type in angle bracket, which can be any legal data type) is defined, but the initial value is not given, and its value is uncertain.
   (2)vector<int> a(10,1); //The vector of 10 integral elements is defined, and the initial value of each element is 1
   (3)vector<int> a(b); //Using b vector to create a vector, the whole replicability assignment
   (4)vector<int> a(b.begin(),b.begin+3); //The a value is defined as the 0-2 of 3 elements in b
   (5)int b[7]={1,2,3,4,5,9,8};
        vector<int> a(b,b+7); //Get initial value from array

2. Several important operations of vector object are illustrated as follows:

    (1)a.assign(b.begin(), b.begin()+3); //b is a vector. Assign a vector composed of 0 ~ 2 elements of b to a
    (2)a.assign(4,2); //Yes a has only 4 elements, and each element is 2
    (3)a.back(); //Returns the last element of a
    (4)a.front(); //Returns the first element of a
    (5)a[i]; //Returns the ith element of a, if and only if a[i] exists 2013-12-07
    (6)a.clear(); //Empty elements in a
    (7)a.empty(); //Judge whether a is empty, return true if it is empty, return false if it is not empty
    (8)a.pop_back(); //Delete the last element of a vector
    (9)a.erase(a.begin()+1,a.begin()+3); //Delete the first (from 0) to the second element in a, that is, the deleted element starts from a.begin()+1 (including it) to a.begin() + 3 (excluding it)
    (10)a.push_back(5); //Insert an element after the last vector of a with a value of 5
    (11)a.insert(a.begin()+1,5); //Insert the value 5 in the position of the first element of a (from the 0 th), for example, a is 1,2,3,4, and after inserting the element, it is 1,5,2,3,4
    (12)a.insert(a.begin()+1,3,5); //Insert 3 numbers in the position of the first element of a (counting from the 0) with values of 5
    (13)a.insert(a.begin()+1,b+3,b+6); //b is an array. Insert the third element of b to the fifth element (excluding b+6) at the first element of a (counting from the 0th). For example, b is 1,2,3,4,5,9,8, and then 1,4,5,9,9,2,3,4,5,9,8
    (14)a.size(); //Returns the number of elements in a;
    (15)a.capacity(); //Returns the total number of elements a can hold in memory
    (16)a.resize(10); //Adjust the number of existing elements of a to 10, more elements will be deleted, less elements will be supplemented, and their values will be random
    (17)a.resize(10,2); //Adjust the number of existing elements of a to 10, delete more elements, and supplement less elements, with a value of 2
    (18)a.reserve(100); //Expand a's capacity to 100, that is to say, when testing a.capacity(); now, the return value is 100. This kind of operation is meaningful only when a large amount of data needs to be added to a, because it will avoid multiple memory capacity expansion operations (when a's capacity is insufficient, the computer will automatically expand, which will certainly reduce the performance) 
    (19)a.swap(b); //b is the vector, which exchanges the elements in a and b as a whole
    (20)a==b; //b is a vector, and there are! =, > =, < =, ><

3. Several ways to access vector s in sequence are illustrated as follows:
(1) add elements to vector a

vector<int> a; 
for(int i=0;i<10;i++) 

2. You can also select elements from the array to add to the vector

int a[6]={1,2,3,4,5,6};
vector<int> b;
for(int i=1;i<=4;i++) 

3. You can also select elements from existing vectors to add to them

int a[6]={1,2,3,4,5,6}; 
vector<int> b; 
vector<int> c(a,a+4); 
for(vector<int>::iterator it=c.begin();it<c.end();it++) 

4. You can also read elements from the file and add them to the vector

ifstream in("data.txt"); 
vector<int> a; 
for(int i; in>>i) 

5. Misunderstanding

vector<int> a; 
for(int i=0;i<10;i++) 

//This and similar practices are wrong. At first, I also made this mistake. Later, I found that subscripts can only be used to get existing elements, but now a[i] is still an empty object

(2) reading elements from vectors
1. Read by subscript

int a[6]={1,2,3,4,5,6}; 
vector<int> b(a,a+4); 
for(int i=0;i<=b.size()-1;i++) 
   cout<<b[i]<<" ";

2. Read through traverser

int a[6]={1,2,3,4,5,6};
vector<int> b(a,a+4);
for(vector<int>::iterator it=b.begin();it!=b.end();it++)
    cout<<*it<<" ";

IV. several important algorithms need to include header files when using:

(1)sort(a.begin(),a.end()); //Arrange the elements in a from a.begin() (including it) to a.end() (excluding it) from small to large
(2)reverse(a.begin(),a.end()); //Invert the elements in a from a.begin() (including it) to a.end() (excluding it), but do not arrange them. For example, the elements in a are 1,3,2,4, and after inversion are 4,2,3,1
(3)copy(a.begin(),a.end(),b.begin()+1); //Copy the elements from a.begin() (including it) to a.end() (excluding it) in a to B, and start copying from b.begin()+1 (including it), overwriting the original elements
(4)find(a.begin(),a.end(),10); //Find 10 in the element from a.begin() (including it) to a.end() (excluding it) in a, and return its position in the vector if any


V. reference:

1. Bear's world


3. C + + Programming Course (Second Edition), Qian Neng, Tsinghua University Press


Posted by tidalwave on Wed, 06 Nov 2019 15:14:25 -0800