vectors


implement the comp201 vector int class, a simpler version of a
vector<int>.

The comp201 vector int class has three constructors. The default constructor creates an empty vector
(capacity=0, size=0).
The second constructor takes an integer argument, which specifies both the initial
capacity and size (with all elements initialized to 0).
The third constructor takes two integer arguments:
the first specifies the initial capacity/size (like the second constructor), while the second argument specifies
the initial value of all the elements.
The class also has a destructor, which deallocates the internal
array.
There are several member functions to access the state of the comp201 vector int class. The capacity
function returns an integer, the current capacity of the vector.
Similarly the size function returns an
integer, the current size. The get function takes as arguments a integer index and an integer value, by
reference, and returns a boolean; if the index is valid (i.e. 0 ≤ index ≤ size − 1), the value is set to the
element in that index of the array, and true is returned, otherwise false is returned.


Let me start with the add function, as this seems the most confusing to me right now, or should I start somewhere else?
Last edited on
Notice the 3 data members: mySize, myCapacity and myArray. Make sure you understand what these are.

First write the constructors and destructor. Then just work your way through the other members. It's actually not very hard. The hardest ones are add() and reverse(). I suggest saving those for last.
Hi,
I wanted to know if what I did is correct, if not, how should I change it...

Thanks!

1
2
3
4
5
6
7
8
9
10
11
void comp201_vector_int::add(int value)
{
	
	
}

ostream& operator <<(ostream& os, const comp201_vector_int& v)
{
	
	
}


void add(int value) should do: size is increased by 1, element at largest index=value
if the new size > capacity, capacity is doubled (if it was 0, now 1)

(ostream& os, const comp201_vector_int& v) should: text representation of state of v is sent to os, and returns os.
Last edited on
I fixed it to this.

When I run the program I get an error: the program stops running. At first I was getting a heap error, but I fixed something, so now it just says the file has stopped working.......

Thanks


1
2
3
4
5
6
7
8
9
10
void comp201_vector_int::add(int value)
{
	
}

ostream& operator <<(ostream& os, const comp201_vector_int& v)
{
	
	return os;
}
Last edited on
@gmac:
dhayden wrote:
Notice the 3 data members: mySize, myCapacity and myArray. Make sure you understand what these are.


Show your constructors too.
If mySize > myCapacity then you need to do more than just set the myCapacity member. You need to create a new array with the capacity, copy the old array elements to the new array, and delete the old array.
Thanks for your suggestions, I will try this out.
Topic archived. No new replies allowed.