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

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;
}
原文地址:https://www.cnblogs.com/lsgxeva/p/7787260.html