Simplify some code using iterators.

* Replace std::distance(X.begin(), X.end()) -> X.size()
* Replace X.begin() == X.end() -> X.empty()
* Prefer preincrement, as recommended by LLVM Coding Standards