Is there a way to define variables of two different types in a for loop initializer?

后端 未结 13 728
余生分开走
余生分开走 2020-12-19 06:12

You can define 2 variables of the same type in a for loop:

int main() {
  for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
    cout << j << e         


        
13条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-19 06:50

    This one is also ugly, but provides also some general way for declaring multiple variables with some given name and types in a for-loop

    int main() {
      for (struct { int i; float j; } x = { };
           x.i < 10; x.i += 1, x.j = 2 * x.i) {
        cout << x.j << endl;
      }
    }
    

提交回复
热议问题