首页 > 编程语言 > 详细

c++11 基于范围的for循环

时间:2017-11-05 14:17:46      阅读:235      评论:0      收藏:0      [点我收藏+]

c++11 基于范围的for循环

 

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <map>

int func(int a[]) // 形参中数组是指针变量,无法确定元素个数
{
    for (auto e: a) // err, 编译失败, 无法找到合适的begin函数
    {
        std::cout << e << " "
    }
    std::cout << std::endl;
}


void mytest()
{
    int a[5] = {1,2,3,4,5};
    // 使用基于范围的for循环
    for (int & e: a)
    {
        e *= 2;
    }
    for (int & e: a)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;

    // 导致内部编译器错误
    func(a);

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

 

c++11 基于范围的for循环

原文:http://www.cnblogs.com/lsgxeva/p/7787260.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!