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

后端 未结 13 729
余生分开走
余生分开走 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 07:02

    Well, it's ugly. But you could use pair.

    int main() {
      for (std::pair p(0,0.0f); 
           p.first < 10; 
           p.first += 1, p.second = 2*p.first) {
        cout << p.second << endl;
      }
    }
    

提交回复
热议问题