C++ for 循环

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     }

 

posted @ 2015-04-07 09:40  扑通  阅读(560)  评论(0编辑  收藏  举报