Vector c summarized in several cases ++ Vector (vector pointer, pointer vector)

1. Standard Library vector type 
vector is a collection of the same type of objects, each object has a corresponding integer index. The standard library will be responsible for managing the storage elements associated with memory. We called the vector container, because it can contain other objects. All objects in a container must be of the same type. 
Before a vector, you must include the appropriate header file. 

#include <Vector> 
. 1 
the using STD :: Vector; 
Vector class is a template (class template). Using a template can be a class definition or function definition, and a plurality of different data types. Therefore, we can define the vector objects saved string, or save vector int value, but also save the class or type of object vector custom. We can just simply understand how the class template definition when using a class template. Statement from the objects of a certain type of class templates generated by the need to provide additional information, the type of information depends on the template. In vector, for example, must explain what type of stored vector object, to specify the type by the template type in the class name following angle brackets: 

vector <int> ivec; // ivec holdsobjects of type int 
vector <the Sales_item> Sales_vec ; // holds Sales_items 
. 1 
2
Note: the same definitions and other variables (variables defined as viewed, such as int a; float b;), is defined to specify a list of target vector and a type of variable. The first defined above, is the type of vector (int equivalent, or Double), i.e., a vector which contains several types of objects of type int, the variable named ivec (corresponding to a, b). The second definition of a variable name is Sales_vec, it holds the elements is an object of type Sales_item. That is, 
the definition method: vector <type> Name 
vector <Type>: As a whole, the type, equivalent int, float. 
Name corresponds to the variable name, a, b. 
It is established in the following 

vector <int> k; // Vector 
vector <int *> kk; // int pointer vector, detailed later appropriate 
vector <int> * kkk; // vector vector pointer 
vector <int *> * kkkk; // int pointer to the vector (compare int * p appreciated, the foregoing pointer variable "*" indicates the type of the variable is a pointer variable, p is a pointer variable name instead of P *) 
vector not a data type, but only a template class, can be used to define any of a variety of data types. Each vector type specifies the type of save elements. Thus, vector <int> and the vector <string> are data types. 

2.Vector vector several cases are summarized 
** 1) vector <int> k ; // vector ** 
. 1 
#include <the iostream>   
#include <  
int main()  
{  
    vector<int> k;  
    for (int j = 0; j<12; j++)  
    {  
        k.push_back(j);//向kk中追加值  
    }  
    for (int j = 0; j<12; j++)  
    {  
        cout <<k[j] << " ";  
    }  
    system("pause");  
    return 0;  
}  
**2)vector <vector> kk;//int指针的向量**
#include<iostream>  
#include<vector>  
using namespace std;  
int main()  
{  
    vector<int*> k;  
    int *p = new int[15];  
    for (int j = 0; j<15; j++)  
    {  
        p[j] = j;  
        k.push_back (& P [J]);  
        for (int j = 0; j<10; j++)  
    }  
    for (int I = 0; I <15; I ++)   
    {   
        COUT << K * [I] << ""; // is inside the container because the vector int type pointer variable,   
    } // pointer values are so, indirect access operator is required *   
    Delete [] P;   
    System ( "PAUSE");   
    return 0;   
}   

**. 3) vector <int> * KKK; // vector vector pointer ** 
. 1 
#include <the iostream>   
#include < vector>   
the using namespace STD;   
int main ()   
{   
    vector <int> * K; // pointer vector vector   
    K = new new vector <int> [. 5];   
    // equivalent int * p = new int [5 ]; i.e., vector <int> * = KKK new new Vector <int> [. 5];   
    for (int I = 0; I <. 5;  ++ I) 
    {   
        {   
            K [I] .push_back (J); // pointer additional image vectors  
        }  
    }  
    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<int*> *kkkk;//int指针的向量指针**
[cpp] view plain copy
#include<iostream>  
#include<vector>  
using namespace std;  
int main()  
{  
    vector<int*> *k;//int指针的向量指针  
    k = new vector<int*>[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");  
}  

  

Guess you like

Origin www.cnblogs.com/CodeWorkerLiMing/p/11221367.html
Recommended