newbie needs help again: deleting a row of a 2D array in C++

Soldato
Joined
15 Feb 2011
Posts
10,234
Location
Slough
i currently have an array with 4 columns and a user defined number of rows set up in the following way:

Code:
int **points;
	points = new int*[num_points];
	for (int i = 0; i < num_points; i++)
	{
		points[i] = new int[4]; //WIDTH
	}
*this may or may not have been stolen from the internet

in my program i need to delete a row of my choice from the array. if i absolutely have to i will use a bubble sort type method to shift all the elements down one place in the array but i'm sure there must be an easier method.

having looked around the internet a bit theres the "delete points[element]" function that works quite nicely for 1D arrays, but i have no idea how to make it work how i would like to for 2D arrays.

if i were to delete row 19 i would need what was row 20 to become row 19, what was row 21 to become row 21 and so on for a variable number of rows. i cannot have it saying that row 19 is an empty row

thanks for any help you can give me :)
 
looking around the interweb makes me think a list of structs should be perfect for what im doing, which makes me very surprised that we have barely covered lists in our lectures.

thanks for the advice guys, but i fear i may be back here a lot more with many problems getting the list to work
 
Do you want constant time [O(1)] random access? If so a linked list is not going to work for you. If that's not important, then yes, a list will give you easy deletion from the middle.

BTW, with the way your original array is structured, deletion of a row is a matter of shifting pointers to higher rows down, and deleting the target row. This is not actually too bad as you don't copy much data. Put it on paper to make it clearer.
 
Do you want constant time [O(1)] random access? If so a linked list is not going to work for you. If that's not important, then yes, a list will give you easy deletion from the middle.

BTW, with the way your original array is structured, deletion of a row is a matter of shifting pointers to higher rows down, and deleting the target row. This is not actually too bad as you don't copy much data. Put it on paper to make it clearer.

you sir have made me a happy bunny. ive spent several hours looking at linked lists and hated every minute of them.

constant time access isnt that important. it would be nice, but i honestly couldnt care if it was access via carrier pigeon, just as long as i get my soft cuddly arrays back :p

would your method work in the way i posted above, or would the numbering of the arrays get messed up, because for what i have in mind i will be deleting lots of rows.

also, sorry to be a pain but could you give me some pointers (whaay!) on how to do your method please. i understand roughly what your doing, but me, pointers and C++ dont mix
 
Last edited:
sorry for bumping this so quickly, but i need to get the entire program and write up done by thursday, and i havent even started on the GUI yet, so any help on that thing azteched mentioned would be great

*edit*
ive figured out that i can deallocate the memory for the row by doing this:
Code:
delete [] points[n];
where n is whatever row i choose, but that row still exists in the array, so when i call it it brings up whatever is stored in those points. any way of making the numbering of the array sort itself out other than the obvious moving every single element around?
 
Last edited:
Back
Top Bottom