Resize function help.

So I need a resize function that is only called when the insert function checks for and discovers that used == capacity. It should increase the size of the array by five. Is this right?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

class Numbers
{
 	public:
                Numbers();
		void insert(int item);
		void resize();
		void display();
		Numbers(const Numbers & other);
		void operator = (const Numbers& other);
	private:
		unsigned long * data;
		std::size_t used;
		std::size_t capacity;
		
};

Numbers::Numbers(){
	used = 0; capacity = 5;
	data = new int [capacity];
	
}
void Numbers::insert(int item){
	if (used == capacity)resize();
	data[used]=item;++used;
}
void Numbers::resize(){
	unsigned long * tmp;
	tmp = new int [capacity +5];
	copy(data, data+used, tmp);
	capacity += 5;
	delete []unsigned long;
	data=tmp;
}
Unless you are simply wanting to learn the copy function, I would try replacing your resize function with the vector library functions as found here:
http://www.cplusplus.com/reference/vector/vector/

Otherwise, everything looks good.
Last edited on
Topic archived. No new replies allowed.