And now, a little demo / test program for class Vector ...
// test_Vector3.h.cpp // // 2016-02-12 //
// Note: this class Vector, with iterator, and const_iterator,
// uses the C++ <algorithm> library 'sort' and 'find' functions OK. //
#include "Vector3.h" // note: also includes <iostream>
#include <string>
#include <iomanip> // re. setw
#include <algorithm> // re. testing here, with this Vector, C++ library sort & find //
const int W = 50;
// re. library sort (reversed order) //
bool reverseCmp( const int& a, const int& b )
{
return b < a;
}
// re. printing out Vector in reversed order //
template< typename T >
void output_rev( const Vector< T >& v, std::ostream& os = std::cout, char end = ' ' )
{
typename Vector< T >::const_iterator it = v.end(), stop = v.begin() ;
--it, --stop;
for( ; it != stop ; -- it )
os << *it << end;
}
int main()
{
using namespace std;
int ary[] = { 9, 8, 6, 0, 1, 4, 7, 3, 2, 5 }; // get some test data ...
const int ary_size = sizeof ary / sizeof *ary;
// select Vector constructor for an (int) array passed in
Vector < int > v( ary, ary + ary_size );
cout << left << setw(W) << "Showing Vector v: ";
v.output( cout, ' ' );
sort( v.begin(), v.end(), reverseCmp ); // erase at index 0, 1 elemement ...
cout << '\n' << setw(W) << "After sort(v.begin(), v.end(), reverseCmp ): ";
v.output( cout, ' ' );
cout << '\n' << setw(W) << "Showing Vector v (back to front): ";
output_rev( v );
sort( v.begin(), v.end() );
cout << '\n' << setw(W) << "After sort(v.begin(), v.end() ): ";
v.output( cout, ' ' );
cout << '\n' << setw(W) << "Showing Vector v (back to front): ";
output_rev( v );
cout << "\n\n";
int val = -1;
Vector< int >::iterator it = find( v.begin(), v.end(), val );
if( it == v.end() )
{
cout << val << " was NOT found in: ";
v.output( cout, ' ' );
cout << '\n';
}
val = 5;
it = find( v.begin(), v.end(), val );
if( it != v.end() )
{
cout << val << ", i.e. *it = " << *it << " was found at " << it;
v.erase( it );
cout << "\nAfter erasing " << val << ", v is now: ";
v.output( cout, ' ' );
cout << ", v.capacity() = " << v.capacity()
<< ", v.size() = " << v.size() << "\n";
}
val = 2;
it = find( v.begin(), v.end(), val );
if( it != v.end() )
{
cout << val << ", i.e. *it = " << *it << " was found at " << it << endl;
v.erase( it, 6 );
cout << "After erasing " << val << " and 5 more, v is now: ";
v.output( cout, ' ' );
cout << ", v.capacity() = " << v.capacity() << ", v.size() = " << v.size() << '\n';
}
v.insert( 2, -1, 3 );
cout << '\n' << setw(W) << "After v.insert( 2, -1, 3 ): ";
v.output( cout, ' ' );
cout << "\nv.capacity() = " << v.capacity() << ", v.size() = " << v.size();
v.reserve(13);
cout << '\n' << setw(W) << "After v.reserve(13): ";
v.output( cout, ' ' );
cout << "\nv.capacity() = " << v.capacity() << ", v.size() = " << v.size();
v.resize(2);
cout << '\n' << setw(W) << "After v.resize(2): ";
v.output( cout, ' ' );
cout << "\nv.capacity() = " << v.capacity() << ", v.size() = " << v.size();
v.clear();
cout << "\nAfter calling v.clear() ... v.empty() = "
<< (v.empty() ? "true" : "false") << '\n';
const string names[] = { "Andy", "Bill", "Jill", "Lucy", "Marie", "Sam", "Sharon", "Zoe" };
const int num_names = sizeof names / sizeof *names;
Vector< string > vNames( names, names + num_names );
cout << "\nTesting pop_back: ";
while( !vNames.empty() )
{
cout << vNames.back() << ' ';
vNames.pop_back();
}
cout << "\n\nPress 'Enter' to continue/exit ... " << flush;
cin.get();
}