C ++ vector demo

Source: Internet
Author: User

# Include
# Include // Contains Vector
Using namespace STD; // specify the namespace
Int main ()
{
Cout

//////////////////////////////////////// /////
// Define a vector
//////////////////////////////////////// /////
Vector Vect;
Vector Vect1 (12); // 12 INT-type elements. The initial values of each element are 0.
Vector Vect2 (); // 12 INT values. The initial value is 9.

//////////////////////////////////////// /////
// Use an array to initialize the Vector
//////////////////////////////////////// /////
Int A [] = {, 0 };
// Vector (,). Element 1, 2, 3 in VT execution
Vector Vt (a + 1, A + 4 );
// Press 3 values at the end
Vt. push_back (1 );
Vt. push_back (2 );
Vt. push_back (3 );
// Define the iterator
Vector : Iterator iter = vt. Begin (); // start address
Vector : Iterator iter_end = vt. End (); // end address. Both addresses are pointer types.
// Traverse vt
For (; iter! = Iter_end; ITER ++)
{
Cout}

//////////////////////////////////////// ///////
// An element is displayed.
//////////////////////////////////////// ///////
Vt. pop_back (); // execution pop-up
// Obtain the start and end addresses in the following two rows
Iter = vt. Begin ();
Iter_end = vt. End ();
Cout for (; iter! = Iter_end; ITER ++)
{
Cout}
//////////////////////////////////////// ////////
// Insert element
//////////////////////////////////////// ////////
Cout // insert format: vector. insert (,); if the number of inserts is 1, the second parameter can be omitted.
Vt. insert (vt. Begin () + 1, 3, 9 );
Iter = vt. Begin ();
Iter_end = vt. End ();
For (; iter! = Iter_end; ITER ++)
{
Cout}

//////////////////////////////////////// ////////
// Delete an element
//////////////////////////////////////// ///////
Cout // Delete format 1: vector. Erase ();
// Deletion Format 2: vector. Erase (,);
Iter = vt. Begin ();
Iter_end = vt. End ();
Vt. Erase (ITER + 1, iter_end); // Delete the second to the last element.
Iter_end = vt. End ();
For (; iter! = Iter_end; ITER ++)
{
Cout}
Return 1;
}

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.