码迷,mamicode.com
首页 > 编程语言 > 详细

c++11之一: 基于范围的for循环

时间:2014-08-05 13:54:09      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   io   for   ar   

 #include <iostream>
 using namespace std;
 int main(){
    int ary[5]{1,2,3,4,5};
     for (int& e: ary)
         e *= 2;
     for (int e: ary)
         cout<<e<<\t;
     cout<<endl;
 }

编译使用: g++ -o for for.cpp -std=c++11

for循环后的括号由冒号":"分成两部分,第一部分是范围内用于迭代的变量,第二部分则表示将被迭代的范围。

基于范围的for循环中迭代的变量可以采用引用的形式,如果迭代变量的值在循环中要改变,否则可以不使用引用。

注意:是否能够使用基于范围的for循环,必须依赖两个条件:

1、for循环迭代的范围是可确定的。

2、迭代的对象实现++和==等操作符。

c++11之一: 基于范围的for循环,布布扣,bubuko.com

c++11之一: 基于范围的for循环

标签:style   blog   color   使用   os   io   for   ar   

原文地址:http://www.cnblogs.com/zxpo/p/3891872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!