gpt4 book ai didi

c++ - C++ 中嵌套 for 循环的替代方案是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 14:30:12 24 4
gpt4 key购买 nike

在 Python 中,代码如下:

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

可以用这样的代码代替:

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

C++ 中是否有一些等价物?

最佳答案

for C 中的循环非常简单并且可以适应各种需要,语法 for (init; condition; increment) 接受所有的函数调用这 3 个字段,允许您模拟任何迭代模式,只需在其中放置代码,调用在每个循环中生成所需信息的函数。

由于这种可能性,像在 Python 中那样使用“范围”是不常见的。在 C 中,在循环时计算每个索引更有意义。

关于c++ - C++ 中嵌套 for 循环的替代方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39950184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com