myVect.size()
returns an unsigned integer. Since the vector is empty it will be 0. When you subtract 1 from that it wraps around and becomes the largest value that std::vector::size_type
(generally std::size_t
) can hold.
Since i
which is 0 is less than that you satisfy the condition and enter the for loop.
Do note that starting in C++11 ranged based for loops were introduced. If you want to loop through a container and use its values you can use
for (const auto & e : container_name)
// use e here in a read only manner.
If you need to modify the elements then you can use
for (auto & e : container_name)
// use e here however you want.
This guarantees that you loop through all of the elements of the container and is generally less error prone.