Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete only first element of array

Is it possible to do something like this:

int *iarray = new int[10];
.....
//do something with it
.....

and then in order to easily remove first element do this:

delete iarray;
iarray++;

it seems that delete (without [] ) still deletes whole array. If it's possible it would be really tricky and clever way to remove first element. The idea is not mine, I saw it somewhere, but it's doesn't work with me. Am I doing something wrong?

like image 854
Marko Avatar asked Feb 18 '23 07:02

Marko


2 Answers

Use a deque to remove an element from the front — that's what this structure is invented for.

like image 85
Alex Reynolds Avatar answered Feb 26 '23 22:02

Alex Reynolds


It looks like you're writing C++, in which case the delete trick is madness and terrible and not going to work. You could certainly just get a pointer to the second element if that's what you really need by doing pointer math, but to actually remove the first item you're going to have to move everything back by one.

like image 42
Ryan Cavanaugh Avatar answered Feb 26 '23 23:02

Ryan Cavanaugh