Array(s)

N

Nickoteen

Hello ..
A begginers question I presume, but it still needs to be asked . .

How can I remove ( or exclude or whatever ) an element from an 1-dimensional
array ?

Let say I have something like this: int myarray [4],
which is then populated with 5 numeric values.

My question is, how can I delete, let's say .. second element of this array
?

Thank you ..
 
J

Jim Langston

Nickoteen said:
Hello ..
A begginers question I presume, but it still needs to be asked . .

How can I remove ( or exclude or whatever ) an element from an
1-dimensional array ?

Let say I have something like this: int myarray [4],
which is then populated with 5 numeric values.

You can't fit 5 numeric values into an array that's only 4 big.
My question is, how can I delete, let's say .. second element of this
array

Simplest answer is, you don't. Next answer is, you shift the remaining
elements left one.

int myarray[4];

myarray[0] = 0;
myarray[1] = 1;
myarray[2] = 2;
myarray[3] = 3;

So how to delete element 1 (the second element)?

for (int i = 2; i <= 3; ++i )
myarray[i - 1] = myarray;
myarray[3] = 0;

This would make the array 0, 1, 3, 0

But, if you need to do this, by all means use std::vector instead. Then you
can simply erase an element out of the middle with a single call.
 
A

Abhishek Srivastava

Nickoteen said:
Hello ..
A begginers question I presume, but it still needs to be asked . .

How can I remove ( or exclude or whatever ) an element from an 1-dimensional
array ?

Let say I have something like this: int myarray [4],
which is then populated with 5 numeric values.

My question is, how can I delete, let's say .. second element of this array
?

Thank you ..
Hi,

A begginers question I presume, but it still needs to be asked . .
You have presumed correctly :)

Well the way to delete an element 'i' from an array of 'n' elements
would be to shift all the elements after i (from i+1 till n) up one
position, and update n to be n - 1, i.e change the total number of
elements.

You can use stl::vectors which will make your life easier with such
things :)

Regards,
Abhishek Srivastava
 
N

Nickoteen

Simplest answer is, you don't. Next answer is, you shift the remaining
elements left one.

int myarray[4];

myarray[0] = 0;
myarray[1] = 1;
myarray[2] = 2;
myarray[3] = 3;

So how to delete element 1 (the second element)?

for (int i = 2; i <= 3; ++i )
myarray[i - 1] = myarray;
myarray[3] = 0;

This would make the array 0, 1, 3, 0


I see. Well, thanks mate .. I'll give it a try ..
 
F

Frederick Gotham

Nickoteen posted:
How can I remove ( or exclude or whatever ) an element from an
1-dimensional array ?


You got a few replies over on comp.lang.learn.c-c++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top