Cisplatin Cisplatin - 1 year ago 110
C++ Question

What is the alternative to nested for loops in C++?

In Python, code like this:

for i in range(1, 10):
for j in range(1, 10):
# Do something here

Can be replaced by code like this:

from itertools import product
for i, j in product(range(1, 10), repeat=2):
# Do something here

Is there some equivalent in C++?

Answer Source

for loops in C are exceedingly simple and can be adapted to a variety of needs, the syntax for (init; condition; increment) accepts function calls in all one of the 3 fields, allowing you to emulate any iteration pattern just by placing code in there calling the functions that generate the information you need on each loop.

Because of that possibility, it is unusual to use "ranges" like in Python. In C it makes more sense to calculate each index as you loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download