Lifetime and masking.
on line 2. On global scope. Accessible from all lines 3-13.
Except on line 8. On line 8 you have a different
. In local scope of loop's body.
A new n is created on every iteration and destroyed by line 9.
Line 8 does not change the n of line 2. Line 11 sees only the n of line 2.
Automatic variable exists only to the end of the scope where it is declared.
Name introduced in inner scope masks (hides) the (same) name of outer scope.
That is not all. On line 7 you do loop one past the end. Not that it matters, for you do nothing with the i.
What does the std::string::find do?
Notice that the find() takes two
arguments. When you write
s.find( 'a' )
, the actual function call is
s.find( 'a', 0 )