If you want to do this very often, write a small useful function:
template<class F>
void repeat(unsigned times, F callback) {
while (times--) callback();
}
for (int i = 0; i < num_pairs; i++) {
repeat(2, [&] { cards.push_back(Card(i)); });
}
I wrote an example on Ideone .
Your first approach may confuse future readers of your code. They might think that the code was there twice by accident. Using this feature avoids confusion.
Achieving performance will be very minimal, even if> 0, since the compiler is likely to build in the function and fully optimize the loop for small ones times. If you are worried about performance, check first.
user1203803
source
share