// Justin C. Miller
// University of Wisconsin Oshkosh
// Made for: http://www.geocities.com/neonprimetime.geo/index.html
// Date: 2-28-2001
// Borland Builder 4.0
// vector, from Standard Template Library
// vector < T >
// vectorname.begin()
// vectorname.clear()
// vectorname.empty()
// vectorname.end()
// vectorname.erase()
// vectorname.insert()
// vectorname.push_back
// vectorname.rbegin()
// vectorname.rend()
// vectorname.size()
#include
#include
using namespace std; // must put this, and no .h's in the #includes
// the following two lines are used to pass a vector to a function
// it is a simple, good example of passing it to a function...
// not that it doesn't matter if your data is an int, or double, or char, etc.
template < class T >
void printVector( const vector< T > &vec) ;
// same thing as above, just reverse printing
template < class T >
void reversePrintVector( vector< T > &vec) ;
int main()
{
const int SIZE = 5 ;
int a[SIZE] = { 41 , 31 , 21 , 11 , 1 } ;
vector< int > my_numbers ; // create a vector
// implementantion of vectorname.empty()
if(my_numbers.empty())
cout << "My STL vector is empty!" << endl ;
// vectorname.size() returns the number of elements in your vector
cout << "The initial size of my vector is ... " << my_numbers.size() << endl ;
// insert my array into my vector but using vectorname.push_back( )
for(int i = 0 ; i < SIZE ; i++)
my_numbers.push_back( a[i] ) ;
cout << "I just inserted 5 numbers..." << endl ;
// watch as the vectorname.empty() has changed states!
if(!my_numbers.empty())
cout << "My STL vector is not empty anymore!" << endl ;
// watch as the size of the vector now changed
cout << "The new size of my vector is ... " << my_numbers.size() << endl ;
cout << "Contents of my vector are ... " << endl ;
/* this is what the print function would look like just in main()
vector< int >:: const_iterator p1 ;
for(p1 = my_numbers.begin() ; p1 != my_numbers.end() ; ++p1)
cout << *p1 << ' ' ;
*/
printVector( my_numbers ) ; // but i'm also going to show you how
// to pass a vector to a function
cout << endl ;
cout << "Contents of my vector, reversed!...." << endl ;
/* this is what the reverse print function would look like just in main()
vector< int >:: reverse_iterator p2 ;
for(p2 = my_numbers.rbegin() ; p2 != my_numbers.rend() ; ++p2)
cout << *p2 << ' ' ;
*/
reversePrintVector( my_numbers ) ; // but i'm also going to show you how
// to pass a vector to a function
cout << endl ;
my_numbers.erase(my_numbers.begin()) ; // erases the beginning element
cout << "I just erased the 1st element" << endl ;
printVector( my_numbers ) ; // print again to see that the beginning was erased
cout << endl ;
my_numbers.insert(my_numbers.begin(), 69) ; // inserts 69 at the beginning
cout << "I just inserted the number 69 at the beginning..." << endl ;
printVector( my_numbers ) ; // print again to see that the beginning was erased
cout << endl ;
my_numbers.clear() ; // clear my vector
cout << "I just cleared my vector of all elements.." << endl ;
if(my_numbers.empty())
cout << "My vector is empty again!" << endl ;
cin >> a[1] ;
return 0 ;
}
template < class T >
void printVector( const vector< T > &vec)
{
vector< T >:: const_iterator p1 ;
for(p1 = vec.begin() ; p1 != vec.end() ; ++p1)
cout << *p1 << ' ' ;
}
template < class T >
void reversePrintVector( vector< T > &vec)
{
vector< T >:: reverse_iterator p2 ;
for(p2 = vec.rbegin() ; p2 != vec.rend() ; ++p2)
cout << *p2 << ' ' ;
}
               (
geocities.com/neonprimetime.geo/cpp)                   (
geocities.com/neonprimetime.geo)