Vector Test


Program Listing

VectorTest.cpp

/************************************************************************
*                                                                       *
*   Program:    VectorTest.cpp                                          *
*                                                                       *
*   Abstract:   This program will test the vector class member          *
*               functions                                               *
*                                                                       *
************************************************************************/

//  Header files

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

using namespace std;

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

int main() {

//  Typedefs

    typedef vector<int>::size_type size_type;

//  Construct a default vector

    cout << "Construct a default vector" << endl;
    vector<int> defaultVec;
    
    cout << "The size of the default vector is " << defaultVec.size() << endl;
    
    if (defaultVec.empty())
        cout << "The default vector is empty" << endl;
    else
        cout << "The default vector is not empty" << endl;

//  Add 5 elements to the default vector

    cout << endl << "Add 5 elements to the default vector" << endl;
    for (int i = 0; i < 5; i++)
        defaultVec.push_back(10*i);
    
    cout << "The size of the default vector is " << defaultVec.size() << endl;
    
    if (defaultVec.empty())
        cout << "The default vector is empty" << endl;
    else
        cout << "The default vector is not empty" << endl;

    cout << "Default vector = ";
    for (int i = 0; i < defaultVec.size(); i++)
        cout << defaultVec[i] << ' ';
    cout << endl;

//  Change the front and back elements

    cout << endl << "Change the first and last elements" << endl;   
    defaultVec.front() = 90;
    defaultVec.back() = 100;
    
    cout << "Default vector = ";
    for (int i = 0; i < defaultVec.size(); i++)
        cout << defaultVec[i] << ' ';
    cout << endl;

//  Remove the last two elements

    cout << endl << "Remove the last two elements" << endl;
    defaultVec.pop_back();
    defaultVec.pop_back();
    
    cout << "Default vector = ";
    for (int i = 0; i < defaultVec.size(); i++)
        cout << defaultVec[i] << ' ';
    cout << endl;

//  Construct an initialized vector of size 8

    cout << endl << "Construct an initialized vector of size 8" << endl;    
    vector<int> initializedVec(8);
    
    cout << "Initialized vector = ";
    for (int i = 0; i < initializedVec.size(); i++)
        cout << initializedVec[i] << ' ';
    cout << endl;

//  Swap the default vector and the initialized vector

    cout << endl << "Swap the default vector and the initialized vector" << endl;
    defaultVec.swap(initializedVec);
    
    cout << "Default vector = ";
    for (int i = 0; i < defaultVec.size(); i++)
        cout << defaultVec[i] << ' ';
    cout << endl;
    
    cout << "Initialized vector = ";
    for (int i = 0; i < initializedVec.size(); i++)
        cout << initializedVec[i] << ' ';
    cout << endl;

//  Resize the initialized vector to 8, filling in with the value 33

    cout << endl << "Resize the initialized vector to 8, filling in with the value 33" << endl;
    initializedVec.resize(8, 33);
    
    cout << "Initialized vector = ";
    for (int i = 0; i < initializedVec.size(); i++)
        cout << initializedVec[i] << ' ';
    cout << endl;

//  Use the at() function to change the first 3 elements of the default vector

    cout << endl << "Use the at() function to change the first 3 elements of the default vector" << endl;
    defaultVec.at(0) = 25;
    defaultVec.at(1) = 35;
    defaultVec.at(2) = 45;
    
    cout << "Default vector = ";
    for (int i = 0; i < defaultVec.size(); i++)
        cout << defaultVec.at(i) << ' ';
    cout << endl;
    
//  Construct a constant vector of size 10, initialized to 99

    cout << endl << "Construct a constant vector of size 10, initialized to 99" << endl;
    const vector<int> constVec((size_type)10, 99);      // Cast is necessary

    cout << "Constant vector = ";
    for (int i = 0; i < constVec.size(); i++)
        cout << constVec.at(i) << ' ';
    cout << endl;

//  Print the first and last elements of the constant vector

    cout << endl << "Print the first and last elements of the constant vector" << endl;
    cout << "First element = " << constVec.front() << endl;
    cout << "Last element = " << constVec.back() << endl;

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

Sample Run

Construct a default vector
The size of the default vector is 0
The default vector is empty

Add 5 elements to the default vector
The size of the default vector is 5
The default vector is not empty
Default vector = 0 10 20 30 40 

Change the first and last elements
Default vector = 90 10 20 30 100 

Remove the last two elements
Default vector = 90 10 20 

Construct an initialized vector of size 8
Initialized vector = 0 0 0 0 0 0 0 0 

Swap the default vector and the initialized vector
Default vector = 0 0 0 0 0 0 0 0 
Initialized vector = 90 10 20 

Resize the initialized vector to 8, filling in with the value 33
Initialized vector = 90 10 20 33 33 33 33 33 

Use the at() function to change the first 3 elements of the default vector
Default vector = 25 35 45 0 0 0 0 0 

Construct a constant vector of size 10, initialized to 99
Constant vector = 99 99 99 99 99 99 99 99 99 99 

Print the first and last elements of the constant vector
First element = 99
Last element = 99

Good-bye



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:05:07 1999 .

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

Return to Coms 261 home page