Sunday, July 12, 2009

How do i remove an element in a c++ array when the array is predetermined?

//say i have a class called Employeetype





//my Employeetype contains an int id, float salary, string last_name, string first_name





Employeetype employee;





employee[30];





//i read the employees in from a file and i have one i would like to remove. i have to remove that employee then shift all the employees down to fill in that space.





//srry im a beginner and need help i need the most simple way possible








//thnx guys

How do i remove an element in a c++ array when the array is predetermined?
You will have to set the element in the array to null, then loop through the rest of the array, moving each up (loop element - 1) until you reach the end of the array.





I have included a link to a site which shows you some great basic array manipulation. Scroll down to the section titled "Inserting" and you will find some code that shows you the moving up of items after an insert. The same thing will apply when you remove an item. Everything below the delete will then move up to take the space of the deleted item.





Just remember that after all items are moved up that the last item is set to null so that you won't have to items that are the same and you have a null that can terminate a loop through the array.





Check out the link and I hope it helps! Good luck!


No comments:

Post a Comment