In: Computer Science
In C++: Set up a vector using an extendable array and name it ArrayVector (use an extendable array). It can be a template/generic class or you can set it up with a certain data type. Use a test driver to try out your ArrayVector by performing various operations.
#include <iostream> using namespace std; class svec { private: string *ds; int len, num; public: svec() { ds = NULL; len = 0; num = 0; } svec(int n) { if(n < 0) { n = 0; } if(n != 0) { ds = new string[n]; } else { ds = NULL; } len = n; num = 0; } svec(int n, string s) { if(n < 0) { n = 0; } if(n != 0) { ds = new string[n]; for(int i=0; i<n; i++) { ds[i] = s; } num = n; } else { ds = NULL; num = 0; } len = n; } int size() { return num; } string front() { if(num == 0) { return ""; } return ds[0]; } string back() { if(num == 0) { return ""; } return ds[num-1]; } string get(int i) { if(i < 0 || i >= num) { return NULL; } return ds[i]; } void cleanup() { delete [] ds; len = 0; num = 0; ds = NULL; } void push_back(string s) { if(len == 0) { len = 10; ds = new string[len]; ds[0] = s; num = 1; return; } else if(num < len) { ds[num++] = s; } else if(len == num) { int newLen = len + 10; string *tmp = new string[newLen]; for(int i=0; i<len; i++) { tmp[i] = ds[i]; } len = newLen; ds[num++] = s; delete [] ds; ds = tmp; } } }; int main() { // this is a string vector implementation. svec myVector; myVector.push_back("First"); myVector.push_back("Second"); myVector.push_back("third"); myVector.push_back("Fourth"); myVector.push_back("Fifth"); myVector.push_back("Sixth"); myVector.push_back("Seventh"); myVector.push_back("Eighth"); cout << "Vector size: " << myVector.size() << endl; for(int i=0; i<myVector.size(); i++) { cout << myVector.get(i) << endl; } cout << "Front: " << myVector.front() << endl; return 0; }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.