Summary of several situations of Vector

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.