When implementing generic code that should operate on top of ranges, the std::advance and std::distance utilities can help.
Both utilities provide constant complexity for random access ranges and fallback to linear operations.
Compiler Explorer link: https://compiler-explorer.com/z/KGda1sd56
@simontoth I think if one is using ranges, std::ranges::advance and std::ranges::distance are wanted. They fix up some things, are constrained to concepts, and work with things like sentinels
@beached_whale Yeah, it's a good default to go for the range versions, except for std::less<> (and the other functors) since the range versions are badly over-constrained.