for my c++ class I was asked to write a class with the following characteristics:
Implement a vector replacement that operates only on integers (you don't need to use templates like the normal STL). Your class should have the following interface:
• A no-argument constructor that allocates a 32-element vector
• A constructor that takes an initial size as the argument
• A method
get
, taking an index and returning the value at that index• A method
set
, that takes an index and a value, and sets the value at that index• A method
pushback
that adds an element to the end of the array, resizing if necessary• A method
pushfront
that adds an element to the beginning of the array• A copy constructor and assignment operator
Your class should not leak memory; any memory it allocates must be deleted. Try to think carefully about how your class can be misused, and how you should handle those scenarios. What do you do if a user gives a negative initial size? What about accessing a negative index?
here is the code:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <assert.h>
class vector
{
private:
int *p;
int seenIndex[100] = {0};
int size = 32;
int largest_ndx = -1;
int elements = 0;
public:
vector();
vector(int size);
vector (const vector& other);
void set(int num, int i);
int get(int i);
void pushback(int i);
void pushfront(int i);
~vector();
vector& operator= (const vector& other);
void print();
};
vector::vector()
{
p = new int[32];
}
vector::vector(int size)
{
assert(size > 0);
this->size = size;
p = new int[size];
}
void vector::set(int num, int i)
{
assert(i >= 0);
/* if index has not been seen, add it (we do not want to update element count if we are inserting in
a previously used index */
if (std::find(std::begin(seenIndex), std::end(seenIndex), i) == std::end(seenIndex))
{
/* ensures pushback method will push element after largest set index */
if (i > largest_ndx)
largest_ndx = i;
seenIndex[elements++] = i;
}
p[i] = num;
}
int vector::get(int i)
{
return p[i];
}
void vector::pushback(int num)
{
/* if size of vector is equal to element count; resize vector */
if (size == elements)
{
size *= 2;
int *temp = new int[size];
for (int i = 0; i < size/2; i++)
temp[i] = p[i];
delete [] p;
p = temp;
p[elements++] =num;
} else if (largest_ndx >= elements) /* if number was set to furthest index, push back after that */
{
p[largest_ndx + 1] = num;
elements++;
} else
{
p[elements++] = num;
}
}
void vector::pushfront(int num)
{
int i = size - 1;
while (i != -1)
{
p[i+1] = p[i];
--i;
}
p[0] = num;
elements++;
}
vector::~vector()
{
delete [] p;
}
vector& vector::operator= (const vector& other)
{
if (this == &other)
return *this; /* returns copy of current object */
/* delete old memory since it is not needed and assign new memory */
delete [] p;
p = new int[other.size];
for (int i = 0; i < other.size; i++)
{
p[i] = other.p[i];
}
for (int i = 0; i < 100; i++)
this->seenIndex[i] = other.seenIndex[i];
this->size = other.size;
this->largest_ndx = other.largest_ndx;
this->elements = other.elements;
return *this;
}
vector::vector (const vector& other)
{
p = new int[other.size];
for (int i = 0; i < other.size; i++)
p[i] = other.p[i];
for (int i = 0; i < 100; i++)
this->seenIndex[i] = other.seenIndex[i];
this->size = other.size;
this->largest_ndx = other.largest_ndx;
this->elements = other.elements;
}
void vector::print()
{
for (int i = 0; i < size; i++)
std::cout << p[i] << " ";
}
Main function:
int main(int argc, const char * argv[]) {
int x = 8;
vector vec(x);
for (int i = 0; i < 5; ++i) {
vec.pushback(i);
}
std::cout << "Original vectors contents: " << std::endl;
vec.print();
std::cout << std::endl;
vector newVec;
newVec = vec;
std::cout << "newVec contents (testing assignment operator): " << std::endl;
newVec.print();
std::cout << std::endl;
newVec.pushfront(100);
std::cout << "newVec contents after pushfront: " << std::endl;
newVec.print();
std::cout << std::endl;
newVec.set(10, 0);
std::cout << "newVec contents after set: " << std::endl;
newVec.print();
std::cout << std::endl;
vector vec3(newVec);
std::cout << "vec3 using copy constructor (taking in newVec): " << std::endl;;
vec3.print();
std::cout << std::endl;
return 0;
}
Output:
Original vectors contents:
0 1 2 3 4 8 5 131072
newVec contents (testing assignment operator):
0 1 2 3 4 8 5 131072
newVec contents after pushfront:
100 0 1 2 3 4 8 5
newVec contents after set:
10 0 1 2 3 4 8 5
vec3 using copy constructor (taking in newVec):
10 0 1 2 3 4 8 5
Program ended with exit code: 0
My program works (as far as I can see), the real thing thats bothering me is I need an array to store the current indices used in the set method. If I don't store them then I cant keep track of how many elements I have, I cant simply increment elements every time I call set due to the fact that the user can set the same index. So basically the user can only use set
a finite amount of times. Also, I am very new to programming and c++ is my first language, I am wondering if I handled the allocated memory properly.
set()
shouldn't do pushback. I believe if the index is out of bounds you should just crash/go into undefined behavior. Also, are you sure it's everything that been asked? Without function to retrieve size it's impossible to iterate over this thing, which makes it barely usable \$\endgroup\$ – Incomputable Nov 18 '16 at 22:10