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