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