Say I have some block of code that I want to execute n times (assuming that n is of type size_t
for simplicity). If I don’t care about the index, what would be the idiomatic way of writing this in C++23?
Currently, I am using
for (auto _ : views::iota(0uz, n)) {
...
}
but I’ve also seen
ranges::for_each(
views::iota(0uz, n),
[] (auto _) {
...
}
);
which is arguably more expressive at the cost of brevity and dealing with lambda captures,
To be clear, this is not for iterating over a container – I am aware that there are more suitable ways of doing that.
viv55 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.