原 C++ Primer学习笔记 - 18 - 字符串、向量和数组 (七)多维数组

3.6 多维数组

C++中多维数组的几个特征
  1. C++中多维数组的本质是数组的数组,这与其他很多语言没有什么区别。
  2. 在初始化时可以按数组的数组的形式进行赋值:
	int ia[3][4] = {
	    {0,1,2,3},
	    {4,5,6,7},
	    {8,9,10,11}
	};
  1. 除了第2条这种方式外,C++还会自动根据位数进行填充,也就是可以写成这样:
	int ia[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11};
  1. 一个小tips,在使用范围for循环时,除了最内层的循环外,所有外层的循环变量都应该使用引用类型,这样可以减小很多的构造开销。
  2. 可以使用类型别名来简化数组的显示,以下是两种类型的声明:
	typedef int int_array[4];//老式风格的别名定义
	using int_array = int[4];//新风格的别名定义
习题
3.43 使用三种方式输出多维数组ia的值,第一种使用范围for循环,第二种使用普通的for循环并且使用下标取值,第三种也使用普通的for循环但要使用指针。要求不使用自动类型推断比如auto、decltype等。

没有什么特别的点,直接帖结果

#include <stdio.h>
#include <iostream>
using namespace std;
int ia[3][4] = {
    {0,1,2,3},
    {4,5,6,7},
    {8,9,10,11}
};

void OutputWithFor()
{
    cout << "output with for" << endl;
    for(size_t i = 0;i < 3;i++ )
    {
        for(size_t j = 0;j<4;j++)
        {
            cout << ia[i][j] << "\t";
        }
        cout << endl;
    }
}

void OutputWithForRange()
{
    cout << "output with for range" << endl;
    for(int (&innerArray)[4] : ia)
    {
        for(int &value : innerArray)
        {
            cout << value << "\t";
        }
        cout << endl;
    }
}

void OutputWithPoint()
{
    cout << "output with point" << endl;
    for(int (*innerArray)[4] = begin(ia); innerArray != end(ia);innerArray++)
    {
        for(int *value = begin(*innerArray); value != end(*innerArray);value ++ )
        {
            cout << *value << "\t";
        }
        cout << endl;
    }
}


int main()
{
    OutputWithFor();
    OutputWithForRange();
    OutputWithPoint();
}
3.44 改写上面的程序,使用类型别名替换循环变量控制的类型。

考察类型别名的使用,也没有什么特别的

#include <stdio.h>
#include <iostream>
using namespace std;
// typedef int int_array[4];
using int_array = int[4];

int_array ia[3] = {
    {0,1,2,3},
    {4,5,6,7},
    {8,9,10,11}
};

void OutputWithFor()
{
    cout << "output with for" << endl;
    for(size_t i = 0;i < 3;i++ )
    {
        for(size_t j = 0;j<4;j++)
        {
            cout << ia[i][j] << "\t";
        }
        cout << endl;
    }
}

void OutputWithForRange()
{
    cout << "output with for range" << endl;
    for(int_array &innerArray : ia)
    {
        for(int &value : innerArray)
        {
            cout << value << "\t";
        }
        cout << endl;
    }
}

void OutputWithPoint()
{
    cout << "output with point" << endl;
    for(int_array *innerArray = begin(ia); innerArray != end(ia);innerArray++)
    {
        for(int *value = begin(*innerArray); value != end(*innerArray);value ++ )
        {
            cout << *value << "\t";
        }
        cout << endl;
    }
}


int main()
{
    OutputWithFor();
    OutputWithForRange();
    OutputWithPoint();
}
3.45 再次改写程序,要求使用auto关键字

直接替换类型就好了

#include <stdio.h>
#include <iostream>
using namespace std;
typedef int int_array[4];

int_array ia[3] = {
    {0,1,2,3},
    {4,5,6,7},
    {8,9,10,11}
};

void OutputWithFor()
{
    cout << "output with for" << endl;
    for(auto i = 0;i < 3;i++ )
    {
        for(auto j = 0;j<4;j++)
        {
            cout << ia[i][j] << "\t";
        }
        cout << endl;
    }
}

void OutputWithForRange()
{
    cout << "output with for range" << endl;
    for(auto &innerArray : ia)
    {
        for(auto &value : innerArray)
        {
            cout << value << "\t";
        }
        cout << endl;
    }
}

void OutputWithPoint()
{
    cout << "output with point" << endl;
    for(auto *innerArray = begin(ia); innerArray != end(ia);innerArray++)
    {
        for(auto *value = begin(*innerArray); value != end(*innerArray);value ++ )
        {
            cout << *value << "\t";
        }
        cout << endl;
    }
}


int main()
{
    OutputWithFor();
    OutputWithForRange();
    OutputWithPoint();
}

一点想法记录

从一开始决定啃C++Pirmer到现在已经过去大半年,原计划今年完成这本书的学习,结果突然工作量爆炸,又跟人合作开发一个小游戏,似乎今年的目标无法达成了,也是很令人蛋疼。
离过年还有两个月,这两个月之内尽可能地完成小游戏的工作,然后继续进行学习吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值