c++第二十一天
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++第二十一天的更多相关文章
- javaSE第二十一天
第二十一天 276 1:字符流(掌握) 276 (1)字节流操作中文数据不是特别的方便,所以就出现了转换流. 276 (2)转换流其实是一个字符流 276 1:InputStr ...
- IT第二十一天 - Collections、ArrayList集合、LinkedList集合、Set集合、HashMap集合、集合的操作注意【修20130828】
NIIT第二十一天 上午 集合 1. 集合Collection存储数据的形式是单个存储的,而Map存储是按照键值对来存储的,键值对:即键+值同时存储的,类似align="center&quo ...
- Python第二十一天 fileinput模块
Python第二十一天 fileinput模块 fileinput模块 fileinput.input([files[, inplace[, backup[, bufsize[, mode[, ...
- 孤荷凌寒自学python第二十一天初识python的类
孤荷凌寒自学python第二十一天初识python的类 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 类是面向对象的编程语言非常重要的概念. 编程语言的进化史中从顺序编程到结构化编程,最后才 ...
- python 学习第二十一天,django知识(三)
一,django的url路由系统总结 1,url(/index/,函数或者类) 2,url(/index/(\d+), 函数或者类) 3,url(/index/(?P<nid>\d+),函 ...
- iOS 进阶 第二十一天(0531)
0531 - Autolayout 不仅可以做屏幕适配还可以做系统适配 uidynamic 做物理动画.能做的效果如下图: Autolayout Autolayout 是一种“自动布局”技术,专门用来 ...
- 第三百二十一天 how can I 坚持
上班第一天,感觉时间过得好慢. 心里好烦,做什么都没心情,感觉没有勇气了,虽然早上说了那么多,但不敢去面对了. 咋整? <猪老三><野子>. 好想去看<美人鱼> 不 ...
- python自动开发之第二十一天
一.请求周期 url> 路由 > 函数或类 > 返回字符串或者模板语言? 1.Form表单提交: 提交 -> url > 函数或类中的方法 - .... HttpResp ...
- 第六百二十一天 how can I 坚持
好歹都是一些集成的框架,也懒得去深入研究了,微信小程序,今天体验了一个,虽然内心非常相信张小龙,但是还是不太看好,但是感觉确实需要一种方式去简化应用程序的开发,今天把没用的app都删了,太耽误时间了, ...
- ThinkPHP第二十一天(JQuery元素获取,parents属性,toggle方法,确认弹出对话框使用)
1.JQuery用法 A:$(function(){code...});表示当页面载入完毕后执行 B:获取元素方法:根据class名称 $('.classname'),根据ID名称 $('#IDnam ...
随机推荐
- OpenGL超级宝典总结(二)2D/3D笛卡尔坐标、坐标裁剪、纹理坐标、MVP转换等概念
如果你想把图形渲染在正确的位置上,那么坐标的设置就很重要了.在OpenGL中,与坐标相关的主要有笛卡尔坐标.坐标裁剪.纹理坐标.MVP(Model View Projection)转换. 1.笛卡尔坐 ...
- intellij idea 2018注册码|intellij idea 2018破解文件下载(附破解教程/汉化包)
intellij idea 2018破解文件http://www.3322.cc/soft/37661.html intellij idea 2018注册码是一款针对“intellij idea 20 ...
- LeetCode——Single Number II
Description: Given an array of integers, every element appears three times except for one. Find that ...
- centos7关闭自动锁屏
centos 7默认几分钟不动就锁屏,实在很讨厌,所以在设置中将其去掉 1.不同的版本应该设置的地方稍有变化,我不敢保证我的这个方法你一定可以用,所以用之前希望你了解你的系统的版本. 2.左上角点击应 ...
- sqlserver 2012隐藏查询结果窗口
1, 08r2的时候都是可以用 ctrl+r隐藏的 到了2012就不行了 方法 . 2, 快捷键的设置
- javascript飞机大战-----005创建子弹对象2
子弹销毁 /* 创建子弹:因为子弹不是只创建一个所以要用构造函数 注意一点:子弹发射的位置应该是英雄机的正中央的位置,所以需要传点东西进来 */ function Bullet(l,t){ this. ...
- 牛客网多校赛第七场J--Sudoku Subrectangle
链接:https://www.nowcoder.com/acm/contest/145/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...
- 字符串函数---atof()函数详解
atof()函数 atof(),是C 语言标准库中的一个字符串处理函数,功能是把字符串转换成浮点数,所使用的头文件为<stdlib.h>.该函数名是 “ascii to floating ...
- lambda 缩写推演
- curl 模拟GET\POST请求,以及curl post上传文件
https://blog.csdn.net/fungleo/article/details/80703365