C ++ vector MSDN Quick Start

Source: Internet
Author: User

// VectorTest. cpp: defines the entry point of the console application.
//


# Include "stdafx. h"


// Empty. cpp
// Compile with:/ESCs
// Adjust strates the vector: empty and vector: erase functions.
// Also demonstrates the vector: push_back function.
//
// Functions:
//
// Vector: empty-Returns true if vector has no elements.
//
// Vector: erase-Deletes elements from a vector (single & range ).
//
// Vector: begin-Returns an iterator to start traversal of
// Vector.
//
// Vector: end-Returns an iterator for the last element of
// Vector.
//
// Vector: push_back-Appends (inserts) an element to the end of
// Vector, allocating memory for it if necessary.
//
// Vector: iterator-Traverses the vector.
//
//////////////////////////////////////// //////////////////////////////


// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is disabled.
# Pragma warning (disable: 4786)


# Include <iostream>
# Include <vector>


Using namespace std;


Typedef vector <int> INTVECTOR;


Const int ARRAY_SIZE = 10;


Void ShowVector (INTVECTOR & theVector );


Int _ tmain (int argc, _ TCHAR * argv [])
{


// Dynamically allocated vector begins with 0 elements.
INTVECTOR theVector;


// Intialize the vector to contain the numbers 0-9.
For (int cEachItem = 0; cEachItem <ARRAY_SIZE; cEachItem ++)
TheVector. push_back (cEachItem );


// Output the contents of the dynamic vector of integers.
ShowVector (theVector );


// Using void iterator erase (iterator Iterator)
// Delete the 6th element (Index starts with 0 ).
TheVector. erase (theVector. begin () + 5 );


// Output the contents of the dynamic vector of integers.
ShowVector (theVector );


// Using iterator erase (iterator First, iterator Last)
// Delete a range of elements all at once.
TheVector. erase (theVector. begin (), theVector. end ());


// Show what's left (actually, nothing ).
ShowVector (theVector );
}


// Output the contents of the dynamic vector or display
// Message if the vector is empty.
Void ShowVector (INTVECTOR & theVector)
{
// First see if there's anything in the vector. Quit if so.
If (theVector. empty ())
{
Cout <"theVector is empty." <endl;
Return;
}


// Iterator is used to loop through the vector.
INTVECTOR: iterator theIterator;


// Output contents of theVector.
Cout <"theVector [";
For (theIterator = theVector. begin (); theIterator! = TheVector. end ();
TheIterator ++)
{
Cout <* theIterator;
If (theIterator! = TheVector. end ()-1) cout <",";
// Cosmetics for the output
}
Cout <"]" <endl;
}

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.