Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Array Question

Status
Not open for further replies.

Cirilia

Programmer
Mar 10, 2003
16
US
How do you remove something from an array? I've done this before, but not for a long time and can't remember.
For example:

int A[5]; //Declare array

Array contains 1, 2, 3, 4, 5

If I know what position the number 2 is at, how do i remove it from the array?
 
Well, you can't really remove a position from an array, what you can do is overwrite all the elements from that position to the end of the array with the one imediatelly on the right.
 
If you want to dynamically add/remove items, I would suggest that you use an STL list, deque, or vector. Then you can use the erase() and other member functions with impunity. Be aware when iterators are invalidated, though.

The advantage is that the STL is platform independent and quite efficient. It also eliminates quite a lot of the normal "housekeeping" code we end up having to do when we manage our own containers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top