So with c++ 20 we get a lot of new features with ranges, spans and so on. Now if i need to iterate over a container, but only the first n
elements, what would be the most appropriate way and is there any practical difference going on behind the scenes? Or is it perhaps a better idea to just go back to regular for loops with indexes as these examples might be less performant?
for (const auto &e: elements | std::ranges::views::take(n)) {
// do stuff
}
for (const auto &e: std::span(elements.begin(), n)) {
// do stuff
}
for (const auto &e: std::ranges::subrange(elements.begin(), elements.begin() + n)) {
// do stuff
}