1. Summary of several situations of vector
Vector <int> k; // vector
Vector <int *> kk; // the vector of the int pointer, which will be considered in detail later
Vector <int> * kkk; // vector pointer
Vector <int *> * kkkk; // vector pointer of the int pointer
Application of various situations:
// Define a vector of the int type and assign a value
Vector <int> k;
Int * v = new int [10];
For (int t = 0; t <10; t ++)
{
V [t] = t;
K. push_back (v [t]); // append a value to k
}
// Define a vector of the int * type and assign a value
Vector <int *> kk;
Int * t = new int [10];
For (int j = 0; j <10; j ++)
{
T [j] = j;
Kk. push_back (& t [j]); // append a value to kk
}
// Description, kk. push_back (& t [j]) is correct. push_back (t) is incorrect because push_back (* const type) and t is a pointer.
// Int-type vector pointer
Vector <int> * kkk;
Kkk = new vector <int> [5];
For (int bb = 0; bb <5; bb ++)
{
For (int a = 0; a <10; a ++)
{
Kkk [bb]. push_back (a); // append value in the Image Vector pointer
}
}
// Output
For (int a = 0; a <k. size (); a ++)
{
Cout <"k [" <a <"] =" <k [a] <endl;
}
For (int a = 0; a <kk. size (); a ++)
{
Cout <"kk [" <a <"] =" <* kk [a] <endl;
}
For (int bb = 0; bb <5; bb ++)
{
For (int aa = 0; aa <kkk [bb]. size (); aa ++)
Cout <"kkk [" <bb <"] [" <aa <"] =" <kkk [bb] [aa] <endl;
}
// Release the int pointer in the vector
For (std: vector <int *>: iterator it = kk. begin (); it <kk. end (); it ++)
{
Delete * it;
}
K. clear (); // clear the k Vector
Kk. clear (); // clear the kk Vector
For (int bb = 0; bb <5; bb ++)
{
Kkk [bb]. clear ();
}
Detailed implementation:
// Vector exercises
Int exciseVector ()
{
// Define a vector of int and assign a value
Vector <int> k;
Int * v = new int [10];
For (int t = 0; t <10; t ++)
{
V [t] = t;
K. push_back (v [t]); // append a value to k
}
// Define a vector of int * and assign a value
Vector <int *> kk;
Int * t = new int [10];
For (int j = 0; j <10; j ++)
{
T [j] = j;
Kk. push_back (& t [j]); // append a value to kk
}
Int * tt = new int [5];
For (int j = 0; j <5; j ++)
{
Tt [j] = 2 * j;
Kk. push_back (& t [j]);
}
// Vector pointer
Vector <int> * kkk;
Kkk = new vector <int> [5];
For (int bb = 0; bb <5; bb ++)
{
For (int a = 0; a <10; a ++)
{
Kkk [bb]. push_back (a); // append value in the Image Vector pointer
Kkk [bb]. push_back (2 * );
}
}
// Vector pointer of the pointer type
Vector <int *> * kkkk;
// Output
For (int a = 0; a <k. size (); a ++)
{
Cout <"k [" <a <"] =" <k [a] <endl;
}
For (int a = 0; a <kk. size (); a ++)
{
Cout <"kk [" <a <"] =" <* kk [a] <endl;
}
For (int bb = 0; bb <5; bb ++)
{
For (int aa = 0; aa <kkk [bb]. size (); aa ++)
Cout <"kkk [" <bb <"] [" <aa <"] =" <kkk [bb] [aa] <endl;
}
// Release the int pointer in the vector
For (std: vector <int *>: iterator it = kk. begin (); it <kk. end (); it ++)
{
Delete * it;
}
K. clear (); // clear the k Vector
Kk. clear (); // clear the kk Vector
For (int bb = 0; bb <5; bb ++)
{
Kkk [bb]. clear ();
}
Delete [] t;
Delete [] kkk;
Return 1;
}