p115~p118:

1、区分int *p[4];和int (*p)[4];。前者是整型指针的数组,后者是指向含有4个整数的数组。

2、规避上述问题的方法就是:使用 auto和 decltype。

3、对于a[3][4],有p = &a[0];(相当于p = a;)则 *p 为a[0]。(解引用后仍为地址)

4、使用类型别名提高程序的可读性。

5、优先使用标准库提供的类型而不是C++内置的底层替代品。

练习 3.43

版本1:使用范围for语句

#include<iostream>
using std::cout;
using std::endl;
int ia[][];
int main()
{
// 必须使用引用类型,否则编译无法通过。
for (int (&row)[] : ia) {
for (int cnt : row) {
cout << cnt << endl;
}
}
return ;
}

版本2:使用下标运算符

#include<iostream>
using std::cout;
using std::endl;
#include<cstddef> // 不加也可使用 size_t
int ia[][];
int main()
{
// 使用size_t而非int可以写出扩展性更好的代码(参考网上资料)
for (size_t i = ; i != ; ++i) {
for (size_t j = ; j != ; ++j) {
cout << ia[i][j] << endl;
}
}
return ;
}

版本3:使用指针

#include<iostream>
using std::cout;
using std::endl;
#include<iterator>
using std::begin;
using std::end;
int ia[][];
int main()
{
for (int (*p)[] = begin(ia); p != end(ia); ++p) {
for (int *q = begin(*p); q != end(*p); ++q) {
cout << *q << endl;
}
}
return ;
}

练习 3.44

使用类型别名改写上一个练习中的程序。

1

#include<iostream>
using std::cout;
using std::endl;
int ia[][];
int main()
{
using int_array = int[];
for (int_array &row : ia) {
for (int cnt : row) {
cout << cnt << endl;
}
}
return ;
}

2

#include<iostream>
using std::cout;
using std::endl;
#include<cstddef>
int ia[][];
int main()
{
using index = size_t;
for (index i = ; i != ; ++i) {
for (index j = ; j != ; ++j) {
cout << ia[i][j] << endl;
}
}
return ;
}

3

#include<iostream>
using std::cout;
using std::endl;
#include<iterator>
using std::begin;
using std::end;
int ia[][];
int main()
{
using int_array = int[];
for (int_array *p = begin(ia); p != end(ia); ++p) {
for (int *q = begin(*p); q != end(*p); ++q) {
cout << *q << endl;
}
}
return ;
}

练习 3.45

使用auto改写程序。

1

#include<iostream>
using std::cout;
using std::endl;
int ia[][];
int main()
{
for (auto &row : ia) {
for (auto cnt : row) {
cout << cnt << endl;
}
}
return ;
}

2

#include<iostream>
using std::cout;
using std::endl;
#include<cstddef>
int ia[][];
int main()
{
for (auto i = ; i != ; ++i) {
for (auto j = ; j != ; ++j) {
cout << ia[i][j] << endl;
}
}
return ;
}

3

#include<iostream>
using std::cout;
using std::endl;
#include<iterator>
using std::begin;
using std::end;
int ia[][];
int main()
{
for (auto p = begin(ia); p != end(ia); ++p) {
for (auto q = begin(*p); q != end(*p); ++q) {
cout << *q << endl;
}
}
return ;
}

c++第二十一天的更多相关文章

  1. javaSE第二十一天

    第二十一天    276 1:字符流(掌握)    276 (1)字节流操作中文数据不是特别的方便,所以就出现了转换流.    276 (2)转换流其实是一个字符流    276 1:InputStr ...

  2. IT第二十一天 - Collections、ArrayList集合、LinkedList集合、Set集合、HashMap集合、集合的操作注意【修20130828】

    NIIT第二十一天 上午 集合 1. 集合Collection存储数据的形式是单个存储的,而Map存储是按照键值对来存储的,键值对:即键+值同时存储的,类似align="center&quo ...

  3. Python第二十一天 fileinput模块

    Python第二十一天    fileinput模块 fileinput模块 fileinput.input([files[, inplace[, backup[, bufsize[, mode[, ...

  4. 孤荷凌寒自学python第二十一天初识python的类

    孤荷凌寒自学python第二十一天初识python的类 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 类是面向对象的编程语言非常重要的概念. 编程语言的进化史中从顺序编程到结构化编程,最后才 ...

  5. python 学习第二十一天,django知识(三)

    一,django的url路由系统总结 1,url(/index/,函数或者类) 2,url(/index/(\d+), 函数或者类) 3,url(/index/(?P<nid>\d+),函 ...

  6. iOS 进阶 第二十一天(0531)

    0531 - Autolayout 不仅可以做屏幕适配还可以做系统适配 uidynamic 做物理动画.能做的效果如下图: Autolayout Autolayout 是一种“自动布局”技术,专门用来 ...

  7. 第三百二十一天 how can I 坚持

    上班第一天,感觉时间过得好慢. 心里好烦,做什么都没心情,感觉没有勇气了,虽然早上说了那么多,但不敢去面对了. 咋整? <猪老三><野子>. 好想去看<美人鱼> 不 ...

  8. python自动开发之第二十一天

    一.请求周期 url> 路由 > 函数或类 > 返回字符串或者模板语言? 1.Form表单提交: 提交 -> url > 函数或类中的方法 - .... HttpResp ...

  9. 第六百二十一天 how can I 坚持

    好歹都是一些集成的框架,也懒得去深入研究了,微信小程序,今天体验了一个,虽然内心非常相信张小龙,但是还是不太看好,但是感觉确实需要一种方式去简化应用程序的开发,今天把没用的app都删了,太耽误时间了, ...

  10. ThinkPHP第二十一天(JQuery元素获取,parents属性,toggle方法,确认弹出对话框使用)

    1.JQuery用法 A:$(function(){code...});表示当页面载入完毕后执行 B:获取元素方法:根据class名称 $('.classname'),根据ID名称 $('#IDnam ...

随机推荐

  1. PopupMenu弹出菜单

    CMenu MoviePopupMenu;//声明 MoviePopupMenu.CreatePopupMenu();//创建弹出菜单 根据对象类型增加弹出项 ) // FLASH对象 { CStri ...

  2. 数据提交方式:post和get

    众所周知,在B/S应用程序中,前台与后台的数据交互,都是通过HTML中Form表单完成的.而Form提供了两种数据传输的方式——get和post.           Get请求表示客户端请求一个ur ...

  3. AndroidWear开发之下载SDK[Android W/Android L]

    Android L Developer Preview SDK发布了,但是天朝还是无法更新到.打开SDK Manager依旧一成不变,这时候就需要利器了. 第一步: 打开Goagent,不要说不知道什 ...

  4. 【BZOJ4285】使者 cdq分治+扫描线+树状数组

    [BZOJ4285]使者 Description 公元 8192 年,人类进入星际大航海时代.在不懈的努力之下,人类占领了宇宙中的 n 个行星,并在这些行星之间修建了 n - 1 条星际航道,使得任意 ...

  5. linux 下 安装go

    首先肯定是下载资源包了,链接汇总在http://www.golangtc.com/download,我用的是 http://www.golangtc.com/static/go/go1.4beta1. ...

  6. JSP 通过Session和Cookie实现网站自动登录

    点记住密码 login.jsp String host = request.getServerName(); Cookie cookie = new Cookie("SESSION_LOGI ...

  7. yii 表单如何写,action指向哪里?

    文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...

  8. opencv学习笔记——颜色空间转换函数cv::cvtColor详解

    cv::cvtColor()用于将图像从一个颜色空间转换到另一个颜色空间的转换(目前常见的颜色空间均支持),并且在转换的过程中能够保证数据的类型不变,即转换后的图像的数据类型和位深与源图像一致. 具体 ...

  9. ubuntu16.04下安装opencv3的viz模块

    OPENCV3.0默认是不安装VIZ模块的,那么如何安装呢? 如果已经安装了Opencv,现在要增加VIZ模块,应该再安装一次就可以吧,我没试过,我是卸载了原来的,重新安装的 简单来说,就是要多安装一 ...

  10. Golang学习-第二篇 搭建一个简单的Go Web服务器

    序言 由于本人一直从事Web服务器端的程序开发,所以在学习Golang也想从Web这里开始学起,如果对Golang还不太清楚怎么搭建环境的朋友们可以参考我的上一篇文章 Golang的简单介绍及Wind ...