Pardon the ASCII art, but the idea is that you just shift the parts the array after the element to remove over, overwriting that particular element.
Starting with an array of size 5, like this one, and an instruction to remove element 3:
+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 |
+---+---+---+---+---+ |
Take the fourth element and put it in the third elements place
+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 |
+---+---+-^-+-+-+---+
| |
+---+ |
Then take the fifth element and put it in the fourth element's place
+---+---+---+---+---+
| 0 | 1 | 3 | 3 | 4 |
+---+---+---+-^-+-+-+
| |
+---+ |
We repeat that "shift" until we have copied the last element over towards the beginning, and that's it. Once we've done that, we may simply ignore the last element, and pretend that the size is one less than it was at the beginning.
+---+---+---+---+
| 0 | 1 | 3 | 4 |
+---+---+---+---+ |
Note:
std:: is roughly a "surname" for variables.
When it preceeds a name, like it does in
std::cout
, that's just making sure that we're referring to the right thing named
cout.
See:
http://www.cplusplus.com/forum/beginner/61121/