Summary of several situations of vector vectors in c ++ (vector pointer and pointer vector)
1. Standard Library vector type
Vector is a set of objects of the same type. Each object has a corresponding integer index value. The standard library will be responsible for managing memory related to storage elements. We call vector a container because it can contain other objects. All objects in a container must be of the same type.
Before using vector, the corresponding header file must be included.
# Include
Using std: vector;
Vector is a class template ). You can use a template to write a class definition or function definition for multiple different data types. Therefore, we can define a vector that saves the string object, a vector that saves the int value, or a custom Class Object vector. When using a class template, you only need to understand how the class template is defined. To declare a type of object generated from a class template, additional information must be provided. The type of information depends on the template. Taking vector as an example, we must describe the type of the object stored in the vector, and specify the type by placing the type in angle brackets after the class Template Name:
Vector Ivec; // ivec holdsobjects of type int
Vector Sales_vec; // holds Sales_items
Note: Like other variable definitions (for example, int a; float B;), a vector object is defined to specify a type and a list of variables. The first definition above, type: vector (Equivalent to int, or double). This type is the vector containing several int type objects. The variable name is ivec (equivalent to a, B ). The second defined variable name is Sales_vec, and the elements it stores are objects of the Sales_item type. That is,
Defined as: vector <type> name
Vector <type>: as a whole, it is a type, which is equivalent to int and float.
The name is equivalent to the variable name a and B.
The following are all true
Vector K; // Vector
Vector Kk; // The int pointer vector, which will be considered in detail later
Vector * Kkk; // vector pointer
Vector * Kkkk; // The vector pointer of the int pointer (compared with int * p, "*" in front of the pointer variable indicates that the type of the variable is Pointer variable, and p indicates the name of the pointer variable, instead of * p)
Vector is not a data type, but a class template. It can be used to define any number of data types. Each element of the vector type specifies the type of elements it saves. Therefore, vector And vector All are data types.
2. Summary of several situations of Vector
1) vector K; // Vector
# Include
# Include
Using namespace std; int main () {vector
K; for (int j = 0; j <12; j ++) {k. push_back (j); // append value to kk} for (int j = 0; j <12; j ++) {cout <
2) vector Kk; // int pointer Vector
# Include
# Include
Using namespace std; int main () {vector
K; int * p = new int [15]; for (int j = 0; j <15; j ++) {p [j] = j; k. push_back (& p [j]) ;}for (int I = 0; I <15; I ++) {cout <* k [I] <""; // because the vector container contains int-type pointer variables,} // Therefore, the values are pointers. Therefore, you need to indirectly access the * delete [] p; system ("pause") operator "); return 0 ;}
3) vector * Kkk; // vector pointer
# Include
# Include
Using namespace std; int main () {vector
* K; // vector pointer k = new vector
[5]; // equivalent to int * p = new int [5]; that is, vector
* Kkk = new vector
[5]; for (int I = 0; I <5; I ++) {for (int j = 0; j <10; j ++) {k [I]. push_back (j); // append value in the Image Vector pointer} for (int I = 0; I <5; I ++) {for (int j = 0; j <k [I]. size (); j ++) cout <k [I] [j] <""; cout <endl;} delete [] k; system ("pause ");}
4) vector * Kkkk; // vector pointer of the int pointer
# Include
# Include
Using namespace std; int main () {vector
* K; // vector pointer of the int pointer k = new vector
[5]; int * p = new int [10]; for (int I = 0; I <5; I ++) {for (int j = 0; j <10; j ++) {p [j] = j; k [I]. push_back (& p [j]) ;}}for (int I = 0; I <5; I ++) {for (int j = 0; j <10; j ++) {cout <* k [I] [j] <"" ;}cout <endl ;}delete [] p; delete [] k; system ("pause ");}