Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modern C++ way to repeat code for set number of times

Very simply, is there a simpler way to repeat a block for a certain number of times, where the block inside does not need the counter variable? The trivial solution is of course

for (int i = 0; i < repetitions; ++i) {
  //do your thing, i is not used here
}

However, now that we have ranged for, standard algorithms and other fancy constructs for iterating over containers, in comparison this is actually starting to feel like a lot of boilerplate and details for what should be an even simpler case. For example we're not interested in the variable i at all etc.

The closest thing to a concrete problem is this: when I encounter a for loop such as above, I need to scan through the code block to see if i is actually used, or if it's just a dummy counter. The declaration of a for loop which actually wants to do something with the integers 0 to repetitions - 1 will look identical. So a repeat (n) -type construct would have the extra semantic information that all the iterations will be the same, except for potential side-effects.

One option is to make a template

template<class functor>
repeat(functor fun, unsigned n) {
   for (unsigned i = 0; i < n; ++i)
     fun();
}

and call

repeat([&](){
  //do your thing
}, repetitions)

but this really seems like overengineered overkill for a simple problem. This could be macroized to make the usage a bit nicer, but that certainly won't help with the overengineered feel.

So one valid answer is that I'm on a wild goose chase here, and should just use the good old for loop with counter.

Any standard C++ is fine, including upcoming standards.

Related questions such as How to create a loop in C++ that loops a certain amount of times? and How to create a loop in C++ that loops a certain amount of times? are beginners asking for some way to achieve this, whereas I'm specifically asking for a modern, clean and elegant way to achieve this. c++ repeat N iterations is very close, though the difference here is that I'm asking for any alternatives, not necessarily included in std::.

like image 566
Timo Avatar asked Apr 05 '19 07:04

Timo


People also ask

How do you repeat codes at a certain time?

To loop through a set of code a certain number of times, you can use the range() function, which returns a list of numbers starting from 0 to the specified end number. You haven't learned about functions yet, but you will soon!

How do I repeat a loop in C?

Syntax. The basic syntax for a repeat / until loop looks like this: repeat DoSomething(); DoSomethingElse(); until x ≥ 10; where a conditional expression is specified after the closing until keyword, and a list of statements can be provided between the repeat and until keywords.

Which loop loops a set number of times?

A For Loop executes the section of code inside the loop, called a subdiagram, a set number of times. The count (N) terminal sets the number of times to repeat the subdiagram. One subdiagram repetition is called an iteration.

How do you repeat a number and time in C++?

There's no direct idiomatic way to repeat strings in C++ equivalent to the * operator in Python or the x operator in Perl. If you're repeating a single character, the two-argument constructor (as suggested by previous answers) works well: std::string(5, '.

How many times can a WHILE LOOP REPEAT a code?

This will depend on the number that the user enters. For example: if user enters 3, the codes inside the while loop I have will repeat 3 times. Here is my code:

Is there a simple repeat(n) macro?

REPEAT (N) macro is tempting, but macros are forbiden. If not compiler intrinsic, user defined repeat is not that "simple and elegant". Here is what I might be using:

How to do something “n” times?

In C and C++ the canonical way to do something “n” times (where “n” can be a numerical constant or a numerical variable or any kind of expression that evaluates to a numerical data type) is, for example int i; int n; // Code to get some value of "n" for (i = 0; i < n; i++) {// Loop to do "something" n times // Do "something" }

Is there a modern way to define a counter in C++?

Instead of a modern C++ way, how about an old C way but without an index: Of course you still need a variable for repetitions though. In my opinion, defining a counter is completely inconsistent with human thinking.I gave my suggestion related to stackoverflow.com/questions/55654319/…. Modern does not necessarily means using newest feautures.


1 Answers

Instead of a modern C++ way, how about an old C way but without an index:

while (repetitions--)
    fun();

Of course you still need a variable for repetitions though.

like image 83
r3mus n0x Avatar answered Sep 23 '22 03:09

r3mus n0x