// Instructions: \\\\ // Part 1: Run the test code using the stl vector class. M
ID: 3596218 • Letter: #
Question
// Instructions: \ // 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. \ //\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\ #include <iostream> #include <string> #include <vector> //replace this with your own class when ready //#include "myVector.h" //include this instead of the stl vector class when ready using namespace std; int main() { vector<int> myvecA; vector<int> myvecB(10); vector<int> myvecC(5,-9); vector<string> myvecD(6,"Are we there yet?"); //The size method should return how many items, abstractly, //the vector currently holds. cout << "Vector A size: " << myvecA.size() << endl; cout << "Vector B size: " << myvecB.size() << endl; cout << "Vector C size: " << myvecC.size() << endl; cout << "Vector D size: " << myvecD.size() << endl; //Capacity should report how large the array holding the items is. //This size will be at least that of 'size()', but could be larger. cout << "Vector A capacity: " << myvecA.capacity() << endl; cout << "Vector B capacity: " << myvecB.capacity() << endl; cout << "Vector C capacity: " << myvecC.capacity() << endl; cout << "Vector D capacity: " << myvecD.capacity() << endl; //You can access the items in the array //with the '[]' operator. cout << endl; cout << "Vector B: " << endl; myvecB[3] = 43; myvecB[7] = 17; for(int i=0; i<myvecB.size(); i++) cout << myvecB[i] << endl; cout << endl; cout << "Vector C: " << endl; myvecC[2] = 50; for(int i=0; i<myvecC.size(); i++) cout << myvecC[i] << endl; cout << endl; cout << "Vector D: " << endl; myvecD[5] = "Shut up kids."; for(int i=0; i<myvecD.size(); i++) cout << myvecD[i] << endl; //An important ability of vectors is the ability to push items to the back //of the vector, which may require increasing the capacity behind the scenes. for(int i=0; i<16; i++) myvecA.push_back(2380 + i); cout << endl; cout << "Vector A's size and capacity:" << endl; cout << "Vector A size: " << myvecA.size() << endl; cout << "Vector A capacity: " << myvecA.capacity() << endl; //vector's also have full stack functionality. Consider the "pop_back" method: for(int i=0; i<10; i++) { cout << "About to pop: " << myvecA.back() << endl; myvecA.pop_back(); } cout << endl; cout << "Vector A's size and capacity:" << endl; cout << "Vector A size: " << myvecA.size() << endl; cout << "Vector A capacity: " << myvecA.capacity() << endl; //Here is some addtional demo of the [] operator and how it works, //as well as the "push_back" method. cout << endl; for(int i=0; i<5; i++) myvecB[i] = i*10; myvecB.push_back(9990); myvecB.push_back(9991); myvecB.push_back(9992); myvecB.push_back(9993); myvecB.push_back(9994); for(int i=0; i<myvecB.size(); i++) cout << myvecB[i] << endl; //Now we will see, for a large example, how the vector adjusts its capacity: cout << endl; int max = 100; for(int i=0; i<max; i++) { int x = i*10000; myvecB.push_back(x); cout << "Pushing: " << x << ", size: " << myvecB.size() << ", capacity: " << myvecB.capacity() << endl; } return 0; }
Explanation / Answer
Given below is the implementation of myVector. In order to run the test, I had to modify the #include line and also the 4 decalarations in main were changed from vector to myVector. Rest of the test code is not changed.
myVector.h
#ifndef myVector_h
#define myVector_h
template <typename T>
class myVector
{
private:
T *data;
int vsize;
int vcapacity;
void expand();
public:
myVector();
myVector(int cap);
myVector(int cap, const T &initial);
int size();
int capacity();
void push_back(const T &value);
T& operator[](int index);
T& back();
void pop_back();
~myVector();
};
template <typename T>
myVector<T>::myVector()
{
data = NULL;
vsize = 0;
vcapacity = 0;
}
template <typename T>
myVector<T>::myVector(int cap)
{
data = new T[cap];
vcapacity = cap;
vsize = cap;
for(int i = 0; i < vcapacity; i++)
data[i] = T();
}
template <typename T>
myVector<T>::myVector(int cap, const T &initial)
{
data = new T[cap];
vcapacity = cap;
vsize = cap;
for(int i = 0; i < vcapacity; i++)
data[i] = initial;
}
template <typename T>
int myVector<T>::size()
{
return vsize;
}
template <typename T>
int myVector<T>::capacity()
{
return vcapacity;
}
template <typename T>
void myVector<T>::push_back(const T &value)
{
if(vsize == vcapacity)
expand();
data[vsize++] = value;
}
template <typename T>
myVector<T>::~myVector()
{
if(data != NULL)
delete[] data;
}
template <typename T>
void myVector<T>::expand()
{
if(vcapacity == 0)
vcapacity = 1;
else
vcapacity = 2 * vcapacity;
T *temp = new T[vcapacity];
for(int i = 0; i < vsize; i++)
temp[i] = data[i];
if(data != NULL)
delete []data; //dealloacate old array
data = temp;
}
template <typename T>
T& myVector<T>::operator [](int index)
{
return data[index];
}
template <typename T>
void myVector<T>::pop_back()
{
if(vsize > 0)
vsize--;
}
template <typename T>
T& myVector<T>::back()
{
return data[vsize-1];
}
#endif /* myVector_h */
main.cpp
//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\/
// Instructions: \
// 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. \
//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\//\
#include <iostream>
#include <string>
//#include <vector> //replace this with your own class when ready
#include "myVector.h" //include this instead of the stl vector class when ready
using namespace std;
int main()
{
myVector<int> myvecA;
myVector<int> myvecB(10);
myVector<int> myvecC(5,-9);
myVector<string> myvecD(6,"Are we there yet?");
//The size method should return how many items, abstractly,
//the vector currently holds.
cout << "Vector A size: " << myvecA.size() << endl;
cout << "Vector B size: " << myvecB.size() << endl;
cout << "Vector C size: " << myvecC.size() << endl;
cout << "Vector D size: " << myvecD.size() << endl;
//Capacity should report how large the array holding the items is.
//This size will be at least that of 'size()', but could be larger.
cout << "Vector A capacity: " << myvecA.capacity() << endl;
cout << "Vector B capacity: " << myvecB.capacity() << endl;
cout << "Vector C capacity: " << myvecC.capacity() << endl;
cout << "Vector D capacity: " << myvecD.capacity() << endl;
//You can access the items in the array
//with the '[]' operator.
cout << endl;
cout << "Vector B: " << endl;
myvecB[3] = 43;
myvecB[7] = 17;
for(int i=0; i<myvecB.size(); i++)
cout << myvecB[i] << endl;
cout << endl;
cout << "Vector C: " << endl;
myvecC[2] = 50;
for(int i=0; i<myvecC.size(); i++)
cout << myvecC[i] << endl;
cout << endl;
cout << "Vector D: " << endl;
myvecD[5] = "Shut up kids.";
for(int i=0; i<myvecD.size(); i++)
cout << myvecD[i] << endl;
//An important ability of vectors is the ability to push items to the back
//of the vector, which may require increasing the capacity behind the scenes.
for(int i=0; i<16; i++)
myvecA.push_back(2380 + i);
cout << endl;
cout << "Vector A's size and capacity:" << endl;
cout << "Vector A size: " << myvecA.size() << endl;
cout << "Vector A capacity: " << myvecA.capacity() << endl;
//vector's also have full stack functionality. Consider the "pop_back" method:
for(int i=0; i<10; i++)
{
cout << "About to pop: " << myvecA.back() << endl;
myvecA.pop_back();
}
cout << endl;
cout << "Vector A's size and capacity:" << endl;
cout << "Vector A size: " << myvecA.size() << endl;
cout << "Vector A capacity: " << myvecA.capacity() << endl;
//Here is some addtional demo of the [] operator and how it works,
//as well as the "push_back" method.
cout << endl;
for(int i=0; i<5; i++)
myvecB[i] = i*10;
myvecB.push_back(9990);
myvecB.push_back(9991);
myvecB.push_back(9992);
myvecB.push_back(9993);
myvecB.push_back(9994);
for(int i=0; i<myvecB.size(); i++)
cout << myvecB[i] << endl;
//Now we will see, for a large example, how the vector adjusts its capacity:
cout << endl;
int max = 100;
for(int i=0; i<max; i++)
{
int x = i*10000;
myvecB.push_back(x);
cout << "Pushing: " << x << ", size: " << myvecB.size() << ", capacity: " << myvecB.capacity() << endl;
}
return 0;
}
output
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: 16
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: 16
0
10
20
30
40
0
0
17
0
0
9990
9991
9992
9993
9994
Pushing: 0, size: 16, capacity: 20
Pushing: 10000, size: 17, capacity: 20
Pushing: 20000, size: 18, capacity: 20
Pushing: 30000, size: 19, capacity: 20
Pushing: 40000, size: 20, capacity: 20
Pushing: 50000, size: 21, capacity: 40
Pushing: 60000, size: 22, capacity: 40
Pushing: 70000, size: 23, capacity: 40
Pushing: 80000, size: 24, capacity: 40
Pushing: 90000, size: 25, capacity: 40
Pushing: 100000, size: 26, capacity: 40
Pushing: 110000, size: 27, capacity: 40
Pushing: 120000, size: 28, capacity: 40
Pushing: 130000, size: 29, capacity: 40
Pushing: 140000, size: 30, capacity: 40
Pushing: 150000, size: 31, capacity: 40
Pushing: 160000, size: 32, capacity: 40
Pushing: 170000, size: 33, capacity: 40
Pushing: 180000, size: 34, capacity: 40
Pushing: 190000, size: 35, capacity: 40
Pushing: 200000, size: 36, capacity: 40
Pushing: 210000, size: 37, capacity: 40
Pushing: 220000, size: 38, capacity: 40
Pushing: 230000, size: 39, capacity: 40
Pushing: 240000, size: 40, capacity: 40
Pushing: 250000, size: 41, capacity: 80
Pushing: 260000, size: 42, capacity: 80
Pushing: 270000, size: 43, capacity: 80
Pushing: 280000, size: 44, capacity: 80
Pushing: 290000, size: 45, capacity: 80
Pushing: 300000, size: 46, capacity: 80
Pushing: 310000, size: 47, capacity: 80
Pushing: 320000, size: 48, capacity: 80
Pushing: 330000, size: 49, capacity: 80
Pushing: 340000, size: 50, capacity: 80
Pushing: 350000, size: 51, capacity: 80
Pushing: 360000, size: 52, capacity: 80
Pushing: 370000, size: 53, capacity: 80
Pushing: 380000, size: 54, capacity: 80
Pushing: 390000, size: 55, capacity: 80
Pushing: 400000, size: 56, capacity: 80
Pushing: 410000, size: 57, capacity: 80
Pushing: 420000, size: 58, capacity: 80
Pushing: 430000, size: 59, capacity: 80
Pushing: 440000, size: 60, capacity: 80
Pushing: 450000, size: 61, capacity: 80
Pushing: 460000, size: 62, capacity: 80
Pushing: 470000, size: 63, capacity: 80
Pushing: 480000, size: 64, capacity: 80
Pushing: 490000, size: 65, capacity: 80
Pushing: 500000, size: 66, capacity: 80
Pushing: 510000, size: 67, capacity: 80
Pushing: 520000, size: 68, capacity: 80
Pushing: 530000, size: 69, capacity: 80
Pushing: 540000, size: 70, capacity: 80
Pushing: 550000, size: 71, capacity: 80
Pushing: 560000, size: 72, capacity: 80
Pushing: 570000, size: 73, capacity: 80
Pushing: 580000, size: 74, capacity: 80
Pushing: 590000, size: 75, capacity: 80
Pushing: 600000, size: 76, capacity: 80
Pushing: 610000, size: 77, capacity: 80
Pushing: 620000, size: 78, capacity: 80
Pushing: 630000, size: 79, capacity: 80
Pushing: 640000, size: 80, capacity: 80
Pushing: 650000, size: 81, capacity: 160
Pushing: 660000, size: 82, capacity: 160
Pushing: 670000, size: 83, capacity: 160
Pushing: 680000, size: 84, capacity: 160
Pushing: 690000, size: 85, capacity: 160
Pushing: 700000, size: 86, capacity: 160
Pushing: 710000, size: 87, capacity: 160
Pushing: 720000, size: 88, capacity: 160
Pushing: 730000, size: 89, capacity: 160
Pushing: 740000, size: 90, capacity: 160
Pushing: 750000, size: 91, capacity: 160
Pushing: 760000, size: 92, capacity: 160
Pushing: 770000, size: 93, capacity: 160
Pushing: 780000, size: 94, capacity: 160
Pushing: 790000, size: 95, capacity: 160
Pushing: 800000, size: 96, capacity: 160
Pushing: 810000, size: 97, capacity: 160
Pushing: 820000, size: 98, capacity: 160
Pushing: 830000, size: 99, capacity: 160
Pushing: 840000, size: 100, capacity: 160
Pushing: 850000, size: 101, capacity: 160
Pushing: 860000, size: 102, capacity: 160
Pushing: 870000, size: 103, capacity: 160
Pushing: 880000, size: 104, capacity: 160
Pushing: 890000, size: 105, capacity: 160
Pushing: 900000, size: 106, capacity: 160
Pushing: 910000, size: 107, capacity: 160
Pushing: 920000, size: 108, capacity: 160
Pushing: 930000, size: 109, capacity: 160
Pushing: 940000, size: 110, capacity: 160
Pushing: 950000, size: 111, capacity: 160
Pushing: 960000, size: 112, capacity: 160
Pushing: 970000, size: 113, capacity: 160
Pushing: 980000, size: 114, capacity: 160
Pushing: 990000, size: 115, capacity: 160
Program ended with exit code: 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.