>>does the new version look like this?:
Assuming all elements are shifted forward properly, that's generally how it will look. However, as has been mentioned by grib, if you don't care about preserving the order of the elements (i.e. the array is just acting as a big collection of unsorted items), then it is more efficient to simply swap the element you're 'removing' with the last element, and then decrease the element count. That way the element you want deleted is removed from its position and moved to the 'back', where it will be safely ignored due to the decreased element count, and the element at the 'back' will be moved into the position vacated by the element you just removed so it will still be 'inside' the array.
>>I was under the impression that writing a for loop out like this was sloppy
Quite possibly. I think it's more a matter of style though (oooh, that 's' word!); when I see 'for', I think "this is going to loop for (the condition) number of times", but when I see 'while', I think "This is going to loop until for some reason (the condition) is false". Therefore, in this case, I used a 'for' because the loop should execute (from the current position to the end of the array) times. Also, when a for loop is of the form:
Code:
for(; condition; ++i)
This, to me, is usually a clear indication that the loop is picking up where another has left off - in this case, it is continuing to the end of the array from where the outer loop has 'paused'. Admittedly, this isn't totally clear since it's continuing the outer loop from the inside; it would likely be more intuitive if the if statement checking for equality simply did break; and the nested loop were placed outside. Either way, however, the effect would be pretty much the same.
isn't i out of its scope in the nested for loop you provided in the updated code because it's defined in the first for loop as opposed to at the start of the function where it would be in scope throughout? Or is it within scope as long as it's inside the for loop where i is declared?
In very simplistic terms, an object is in scope as long as the innermost {} block it was defined inside is still open - literally. You can, in fact, do:
Code:
int main()
{
int i = 5;
std::cout << i;
{
int j = 1; //j temporarily brought into scope
std::cout << j;
}
//j now out of scope
return 0;
}
Of course, it's a *little* different in for loops:
Code:
//i is not in scope
for(int i = 0; i < 5; ++i) //i is in scope
{
//i is in scope
}
//i is not in scope
As you can see, the variiable declared in () acts as if it were declared inside the {}. Similarly:
Code:
//i is out of scope
for(int i = 0; i < 5; ++i) //i is in scope
std::cout << i; //i is in scope
//i is out of scope
So basically, i is in scope as long as the code block it was declared in is still open. Since the code block in this example is a single line, i is in scope for that single line.
**EDIT**
Oops. Well, to answer the question :
i is still in scope because, as you guessed, the code block it was defined inside of (the outer for loop) hasn't ended yet.