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. ajax如何上传文件

    PHP: <?php /** * Created by PhpStorm. * User: DELL * Date: 2017/11/23 * Time: 10:57 */ header(&qu ...

  2. Error setting expression 'XXX' with value 设置表达式“XXX”时出错 解决方法

    1.表达式“xxx”在所调用的action里没有与之对应的对象: 2.action里有该对象作为私有成员变量但是没有get&set方法.

  3. Oracle12cWindows安装、介绍及简单使用(图文)

    1.下载 地址为:http://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads/index.html 含企业版和 ...

  4. poj_1456 贪心

    题目大意 一家超市,要卖出N种物品(每种物品各一个),每种物品都有一个卖出截止日期Di(在该日期之前卖出可以获得收益,否则就无法卖出),且每种物品被卖出都有一个收益值Pi. 卖出每个物品需要耗时1天, ...

  5. 卸载vue-cli

    全局安装:npm install vue-cli -g; 全局卸载:npm uninstall vue-cli -g; 查看vue版本,vue -V 回车,查看vue最新的版本.

  6. PreparedStatement vs Statement

    Statement和PreparedStatement之间的区别 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程   2.使用 Statemen ...

  7. 查看、关闭登录到linux的终端

    基本概念: tty(终端设备的统称):tty一词源于Teletypes,原来指的是电传打字机,是通过串行线用打印机键盘阅读和发送信息的东西,后来这东西被键盘和显示器取代,所以现在叫终端比较合适.终端是 ...

  8. CommonHelper 公共类

      public static class CommonHelper 公共帮助类 using System.Collections.Generic; using System.Linq; using ...

  9. python及numpy,pandas易混淆的点

    https://blog.csdn.net/happyhorizion/article/details/77894035 初接触python觉得及其友好(类似matlab),尤其是一些令人拍案叫绝不可 ...

  10. Django - 项目总结

    总结: 基础,进阶,项目 Django - 练习(图书管理系统) 前后端分离得项目: vue + rest framework 项目: 图书增删改查页面 BBS + BLOG系统 CRM系统   在线 ...