C++forサイクル

2589 ワード

C++のもう一つのforループの書き方は、C#のforeach構文と似ていて、配列のタイプを知る必要はありません.
C++:for(auto& item:items)
C#:foreach(var item in items)
 1     int ages [10] = {0};

 2     //  for  

 3     for(int i=0;i<(sizeof(ages)/sizeof(ages[0]));i++)

 4     {        

 5         ages[i]=i;

 6         cout<<ages[i]<<endl;

 7     }

 8     //for(auto     

 9     for(auto& age :ages)

10     {

11         age+=10; 

12     }

13     cout<<"----------"<<endl;    

14     //for(auto   

15     for(auto age :ages)

16     {

17         cout<<age<<endl;

18     }