Replaced most manual `const_iterator`-based loops and some reverse-iterator loops with range loops. Fixes: #18858