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. mySQL数据库三:命令行附录

    一:where 在上一篇,粗略的介绍了where,但是where后面可以跟其他的条件,现在我们来一一说明 1.between:在某两个值之间 我建立一个名为person的表,里面有id,name,ag ...

  2. php学习十一:组合

    我们在类当中我往往会用到一些对象,此时的继承就无法满足我们的需求,这个时候我们需要用到组合.继承如果是is..a的关系,那么组合就是has...a的关系,直接在本类里面声明即可,不过声明的是一个对象 ...

  3. codeblocks中cocos2dx项目添加新的.cpp和.h文件后编译运行的方法

    新建了cocos2dx项目后(比如我这里建立的项目名为Test01),项目目录下有如下目录和文件: bin CMakeLists.txt MyGame.layout proj.win10 Classe ...

  4. OracleClient安装系统环境变量配置

    1.变量名:TNS_ADMIN  值:E:\instantclient_11_2\NETWORK\ADMIN 2.变量名:NLS_LANG    值:SIMPLIFIED CHINESE_CHINA. ...

  5. c++11实现optional

    optional< T> c++14中将包含一个std::optional类,optional< T>内部存储空间可能存储了T类型的值也可能没有存储T类型的值.当optiona ...

  6. 【PHP】通过header发送自定义数据

    发送header: 我们定义了三个参数,token.language.region,放入header发送过去 <?php $url = 'http://www.example.com'; $he ...

  7. 使用synchronized(非this对象)同步代码块解决脏读问题

    首先通过示例来学习验证多个线程调用同一个方法时随机的. package syn_out_asyn; import java.util.ArrayList; import java.util.List; ...

  8. Java初学者笔记四:按行读写文件和输入处理

    一.我们来看python的很简单: 1.读文件: with open("/path/file","r") as fr: for line in fr.readl ...

  9. 使用log4net记录日志到数据库(含有自定义属性)

    记录日志是管理系统中对用户行为的一种监控与审核,asp.net中记录日志的方式有很多种,这里我只介绍一下最近用到的log4net,关于他的具体介绍网上有很多,我讲一下他的用法. 第一步:在配置文件中的 ...

  10. [SQL] sql server中如何查看执行效率不高的语句

    sql server中,如果想知道有哪些语句是执行效率不高的,应该如何查看呢?下面就将为您介绍sql server中如何查看执行效率不高的语句,供您参考.在测量功能时,先以下命令清除sql serve ...