Vector Iterator Test


IteratorTest.cpp

Program listing

/************************************************************************
*                                                                       *
*   Program:    IteratorTest.cpp                                        *
*                                                                       *
*   Abstract:   This program will test the vector class member          *
*               functions that use iterators                            *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>
#include <vector>

using namespace std;

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
*   Purpose:    To test the vector class member functions that use      *
*               iterators                                               *
*                                                                       *
************************************************************************/

int main() {

//  Typedefs

    typedef vector<int>::iterator iterator;
    typedef vector<int>::const_iterator const_iterator;
    typedef vector<int>::reverse_iterator reverse_iterator;
    typedef vector<int>::const_reverse_iterator const_reverse_iterator;

//  Construct a vector

    cout << "Construct a vector" << endl;
    vector<int> vec;
    
//  Add 10 elements to the default vector

    cout << endl << "Add 5 elements to the default vector" << endl;
    for (int i = 1; i <= 10; i++)
        vec.push_back(10*i);
    
    cout << "The size of the default vector is " << vec.size() << endl;

//  Use an iterator to display the vector

    cout << endl << "Use an iterator to display the vector" << endl;
    cout << "Vector = ";
    for (iterator it = vec.begin(); it != vec.end(); it++)
        cout << *it << ' ';
    cout << endl;

//  Use an iterator to display the vector in reverse order

    cout << endl << "Use an iterator to display the vector in reverse order" << endl;
    cout << "Vector = ";
    for (iterator it = vec.end(); it != vec.begin(); it--)
        cout << *it << ' ';
    cout << endl;

//  Use a reverse iterator to display the vector

    cout << endl << "Use a reverse iterator to display the vector" << endl;
    cout << "Vector = ";

    for (reverse_iterator rit = vec.rbegin(); rit != vec.rend(); rit++)
        cout << *rit << ' ';
    cout << endl;

//  Construct a constant vector

    cout << endl << "Construct a constant vector" << endl;
    const vector<int> constVec = vec;
    
//  Use a constant iterator to display the vector

    cout << endl << "Use a constant iterator to display the vector" << endl;
    cout << "Vector = ";
    for (const_iterator cit = constVec.begin(); cit != constVec.end(); cit++)
        cout << *cit << ' ';
    cout << endl;

//  Use a constant reverse iterator to display the vector

    cout << endl << "Use a constant reverse iterator to display the vector" << endl;
    cout << "Vector = ";

    for (const_reverse_iterator crit = constVec.rbegin(); crit != constVec.rend(); crit++)
        cout << *crit << ' ';
    cout << endl;

//  Insert 99 into the 3rd position of the vector

    cout << endl << "Insert 99 into the 3rd position of the vector" << endl;
    iterator it = vec.begin();  // Start at first position
    it++;                       // Increment to second position
    it++;                       // Increment to third position
    vec.insert(it, 99);         // Insert into third position

    cout << "Vector = ";
    for (iterator it = vec.begin(); it != vec.end(); it++)
        cout << *it << ' ';
    cout << endl;

//  Delete value from 2nd position of the vector

    cout << endl << "Delete value from 2nd position of the vector" << endl;
    it = vec.begin();           // Start at first position
    it++;                       // Increment to second position
    vec.erase(it);              // Delete from second position
    
    cout << "Vector = ";
    for (iterator it = vec.begin(); it != vec.end(); it++)
        cout << *it << ' ';
    cout << endl;

    cout << endl << "Good-bye" << endl;
    
    return 0;
}

Sample Run

Construct a vector

Add 5 elements to the default vector
The size of the default vector is 10

Use an iterator to display the vector
Vector = 10 20 30 40 50 60 70 80 90 100 

Use an iterator to display the vector in reverse order
Vector = 3650 100 90 80 70 60 50 40 30 20 

Use a reverse iterator to display the vector
Vector = 100 90 80 70 60 50 40 30 20 10 

Construct a constant vector

Use a constant iterator to display the vector
Vector = 10 20 30 40 50 60 70 80 90 100 

Use a constant reverse iterator to display the vector
Vector = 100 90 80 70 60 50 40 30 20 10 

Insert 99 into the 3rd position of the vector
Vector = 10 20 99 30 40 50 60 70 80 90 100 

Delete value from 2nd position of the vector
Vector = 10 99 30 40 50 60 70 80 90 100 

Good-bye



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Sat Nov 27 14:26:11 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/IteratorTest.html

Return to Coms 261 home page