Remove the range-based for-loops

This PR replaces the range-based for-loops with the iterator-based-for-loops. As Dima pointed out in #365, the range-based for-loops is not supported by IBM XLC.