【发布时间】:2018-01-21 03:09:03
【问题描述】:
我想在 c++ 中做这样的事情:
for (int i = 0, i < 3; ++i)
{
const auto& author = {"pierre", "paul", "jean"}[i];
const auto& age = {12, 45, 43}[i];
const auto& object = {o1, o2, o3}[i];
print({"even", "without", "identifier"}[i]);
...
}
大家都知道怎么做这种把戏吗?我在python中做了很多。 它帮助我很好地分解代码。
【问题讨论】:
-
这看起来像......一个坏主意?为什么每次循环迭代都要构造一个数组来每次都得到一个不同的元素?先在循环外声明
authors={"friedrich johann", "wolfgang", "martin"};,然后在循环中选择const auto &author=authors[i];,有什么问题? -
你是对的,这就是解决方案。