Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: Run the test code using the stl vector class. Make sure you understand t

ID: 674741 • Letter: P

Question

Part 1: Run the test code using the stl vector class. Make sure you understand the output and how the vector class works.

Part 2: Create your own vector class (in the file "myVector.h"). To test your vector class, Swap the #include statements above so that the test code uses your vector class instead of the stl vector class.

YOU MAY NOT CHANGE ANY PART OF THE TEST CODE other than swapping the included files.

Your vector class must yield the exact same results from the test code as the stl vector class, with the following exceptions: The "capacity" method may return different values (i.e., you may choose any resizing method you want, but you should consider what is the smartest approach with respect to efficiency). Additionally, for the 0-parameter constructor and the 1-parameter, you do not need to initialize the values of the vector items (but you must for the 2-parameter constructor). Beyond these items, your code must behave the same as for the stl vector.

Explanation / Answer

Complete Program:


template <class T>
class vector
{
public:
typedef T* Iterator;
    vector();
    vector(int size);
    vector(int size, const T & initial);
    vector(const vector<T>& v);
    ~vector();
void push_back(const T& value);
    void pop_back();
int capacity() const;
    int size() const;
void clear();   
    bool empty() const;
    Iterator begin();
    Iterator end();
    T& front();
    T& back();
    void resize(int size);
    T & operator[](int index);
    vector<T> & operator = (const vector<T> &);
   
private:
    int count;
    int max_size;
    T *arr;
};

template<class T>
vector<T>::vector()
{
    max_size = 0;
    count = 0;
    arr = 0;
}

template<class T>
vector<T>::vector(const vector<T> & v)
{
    count = v.count;
    max_size = v.max_size;
    arr = new T[max_size];
    for (int i = 0; i < max_size; i++)
        arr[i] = v.arr[i];
}

template<class T>
vector<T>::vector(int size)
{
    count = size;
    max_size = size;
    arr = new T[max_size];
for(int i = 0; i < max_size; i++)
  arr[i] = 0;
}

template <class T>
bool vector<T>:: empty() const
{
    return count == 0;
}

template<class T>
vector<T>::vector(int size, const T& initial)
{
    count = size;
    max_size = count;
    arr = new T [max_size];
    for (int i = 0; i < size; i++)
        arr[i] = initial;
}

template<class T>
vector<T>& vector<T>::operator=(const vector<T> & v) {
    delete[] arr;
    count = v.count;
    max_size = v.max_size;
    arr = new T [max_size];
    for (unsigned int i = 0; i < count; i++)
        arr[i] = v.arr[i];
    return *this;
}

template<class T>
typename vector<T>::Iterator vector<T>::begin()
{
    return arr;
}

template<class T>
typename vector<T>::Iterator vector<T>::end()
{
    return arr + size();
}

template<class T>
T& vector<T>::front()
{
    return arr[0];
}

template<class T>
T& vector<T>::back()
{
    return arr[count - 1];
}

template<class T>
void vector<T>::push_back(const T & v)
{
    if (count >= max_size)
{
        resize(count);
    }
    arr[count++] = v;
}

template<class T>
void vector<T>::pop_back()
{
    count--;
}

template<class T>
int vector<T>::size() const {
    return count;
}

template<class T>
void vector<T>::resize(int n)
{
int newSize = n + (n / 2);
T *newBuff = new T[newSize];
for(int i = 0; i < newSize; i++)
{
  if(i < size())
   newBuff[i] = arr[i];
  else
   newBuff[i] = 0;
}

delete [] arr;

arr = new T[newSize];
for(int i = 0; i < newSize; i++)
  arr[i] = newBuff[i];

    count = count;
max_size = newSize;
}

template<class T>
T& vector<T>::operator[](int index)
{
    return arr[index];
}

template<class T>
int vector<T>::capacity()const
{
    return max_size;
}

template<class T>
vector<T>::~vector()
{
    delete[] arr;
}

template <class T>
void vector<T>::clear()
{
    max_size = 0;
    count = 0;
    arr = 0;
}

Sample Ouput:


Vector A size: 0
Vector B size: 10
Vector C size: 5
Vector D size: 6
Vector A capacity: 0
Vector B capacity: 10
Vector C capacity: 5
Vector D capacity: 6

Vector B:
0
0
0
43
0
0
0
17
0
0

Vector C:
-9
-9
50
-9
-9

Vector D:
Are we there yet?
Are we there yet?
Are we there yet?
Are we there yet?
Are we there yet?
Shut up kids.

Vector A's size and capacity:
Vector A size: 16
Vector A capacity: 19
About to pop: 2395
About to pop: 2394
About to pop: 2393
About to pop: 2392
About to pop: 2391
About to pop: 2390
About to pop: 2389
About to pop: 2388
About to pop: 2387
About to pop: 2386

Vector A's size and capacity:
Vector A size: 6
Vector A capacity: 19

0
10
20
30
40
0
0
17
0
0
9990
9991
9992
9993
9994

Pushing: 0, size: 16, capacity: 22
Pushing: 10000, size: 17, capacity: 22
Pushing: 20000, size: 18, capacity: 22
Pushing: 30000, size: 19, capacity: 22
Pushing: 40000, size: 20, capacity: 22
Pushing: 50000, size: 21, capacity: 22
Pushing: 60000, size: 22, capacity: 22
Pushing: 70000, size: 23, capacity: 33
Pushing: 80000, size: 24, capacity: 33
Pushing: 90000, size: 25, capacity: 33
Pushing: 100000, size: 26, capacity: 33
Pushing: 110000, size: 27, capacity: 33
Pushing: 120000, size: 28, capacity: 33
Pushing: 130000, size: 29, capacity: 33
Pushing: 140000, size: 30, capacity: 33
Pushing: 150000, size: 31, capacity: 33
Pushing: 160000, size: 32, capacity: 33
Pushing: 170000, size: 33, capacity: 33
Pushing: 180000, size: 34, capacity: 49
Pushing: 190000, size: 35, capacity: 49
Pushing: 200000, size: 36, capacity: 49
Pushing: 210000, size: 37, capacity: 49
Pushing: 220000, size: 38, capacity: 49
Pushing: 230000, size: 39, capacity: 49
Pushing: 240000, size: 40, capacity: 49
Pushing: 250000, size: 41, capacity: 49
Pushing: 260000, size: 42, capacity: 49
Pushing: 270000, size: 43, capacity: 49
Pushing: 280000, size: 44, capacity: 49
Pushing: 290000, size: 45, capacity: 49
Pushing: 300000, size: 46, capacity: 49
Pushing: 310000, size: 47, capacity: 49
Pushing: 320000, size: 48, capacity: 49
Pushing: 330000, size: 49, capacity: 49
Pushing: 340000, size: 50, capacity: / /
Pushing: 350000, size: 51, capacity: 73
Pushing: 360000, size: 52, capacity: 73
Pushing: 370000, size: 53, capacity: 73
Pushing: 380000, size: 54, capacity: 73
Pushing: 390000, size: 55, capacity: 73
Pushing: 400000, size: 56, capacity: 73
Pushing: 410000, size: 57, capacity: 73
Pushing: 420000, size: 58, capacity: 73
Pushing: 430000, size: 59, capacity: 73
Pushing: 440000, size: 60, capacity: 73
Pushing: 450000, size: 61, capacity: 73
Pushing: 460000, size: 62, capacity: 73
Pushing: 470000, size: 63, capacity: 73
Pushing: 480000, size: 64, capacity: 73
Pushing: 490000, size: 65, capacity: 73
Pushing: 500000, size: 66, capacity: 73
Pushing: 510000, size: 67, capacity: 73
Pushing: 520000, size: 68, capacity: 73
Pushing: 530000, size: 69, capacity: 73
Pushing: 540000, size: 70, capacity: 73
Pushing: 550000, size: 71, capacity: 73
Pushing: 560000, size: 72, capacity: 73
Pushing: 570000, size: 73, capacity: 73
Pushing: 580000, size: 74, capacity: 109
Pushing: 590000, size: 75, capacity: 109
Pushing: 600000, size: 76, capacity: 109
Pushing: 610000, size: 77, capacity: 109
Pushing: 620000, size: 78, capacity: 109
Pushing: 630000, size: 79, capacity: 109
Pushing: 640000, size: 80, capacity: 109
Pushing: 650000, size: 81, capacity: 109
Pushing: 660000, size: 82, capacity: 109
Pushing: 670000, size: 83, capacity: 109
Pushing: 680000, size: 84, capacity: 109
Pushing: 690000, size: 85, capacity: 109
Pushing: 700000, size: 86, capacity: 109
Pushing: 710000, size: 87, capacity: 109
Pushing: 720000, size: 88, capacity: 109
Pushing: 730000, size: 89, capacity: 109
Pushing: 740000, size: 90, capacity: 109
Pushing: 750000, size: 91, capacity: 109
Pushing: 760000, size: 92, capacity: 109
Pushing: 770000, size: 93, capacity: 109
Pushing: 780000, size: 94, capacity: 109
Pushing: 790000, size: 95, capacity: 109
Pushing: 800000, size: 96, capacity: 109
Pushing: 810000, size: 97, capacity: 109
Pushing: 820000, size: 98, capacity: 109
Pushing: 830000, size: 99, capacity: 109
Pushing: 840000, size: 100, capacity: 109
Pushing: 850000, size: 101, capacity: 109
Pushing: 860000, size: 102, capacity: 109
Pushing: 870000, size: 103, capacity: 109
Pushing: 880000, size: 104, capacity: 109
Pushing: 890000, size: 105, capacity: 109
Pushing: 900000, size: 106, capacity: 109
Pushing: 910000, size: 107, capacity: 109
Pushing: 920000, size: 108, capacity: 109
Pushing: 930000, size: 109, capacity: 109
Pushing: 940000, size: 110, capacity: 163
Pushing: 950000, size: 111, capacity: 163
Pushing: 960000, size: 112, capacity: 163
Pushing: 970000, size: 113, capacity: 163
Pushing: 980000, size: 114, capacity: 163
Pushing: 990000, size: 115, capacity: 163
Press any key to continue . . .