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

c++ 遍历多维数组的10种方式

时间:2014-09-02 12:17:14      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   div   log   sp   amp   

int ia[3][4] = {1,2,3,4,5,6,7,8};
//下标
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 4; j++)
    {
        cout << ia[i][j] << endl;
    }
}
//指针
for (int(*p)[4] = ia; p != ia + 3; p++)
{
    for (int *q = *p; q != *p + 4; q++)
    {
        cout << *q << endl;
    }
}    
//指针,迭代
for (int(*p)[4] = begin(ia); p != end(ia); p++)
{
    for (int* q = begin(*p); q != end(*p); q++)
    {
        cout << *q << endl;
    }
}
//范围for
for (int (&p)[4] : ia)
{
    for (int q : p)
    {
        cout << q << endl;
    }
}

typedef int int_array[4];
//类别名+指针
for (int_array* p = ia; p != ia + 3; p++)
{
    for (int* q = *p; q != *p + 4; q++)
    {
        cout << *q << endl;
    }
}
//类别名+迭代
for (int_array *p = begin(ia); p != end(ia); p++)
{
    for (int* q = begin(*p); q != end(*p); q++)
    {
        cout << *q << endl;
    }
}
//类别名+范围for
for (int_array &p : ia)
{
    for (int q : p)
    {
        cout << q << endl;
    }
}
//auto
for (auto p = ia; p != ia + 3; p++)
{
    for (auto q = *p; q != *p + 4; q++)
    {
        cout << *q << endl;
    }
}
//auto+迭代
for (auto p = begin(ia); p != end(ia); p++)
{
    for (auto q = begin(*p); q != end(*p); q++)
    {
        cout << *q << endl;
    }
}
//auto+范围for
for (auto &p : ia)
{
    for (auto q : p)
    {
        cout << q << endl;
    }
}

 

c++ 遍历多维数组的10种方式

标签:style   blog   color   ar   for   div   log   sp   amp   

原文地址:http://www.cnblogs.com/zhangfp/p/3951114.html

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