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 ...
随机推荐
- UITouch 的主要方法:
1. UITouch 的主要方法: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesM ...
- 【linux】Centos下登陆mysql报错#1045 - Access denied for user 'root'@'localhost' (using password: NO)
创建mysql 远程链接 grant all privileges on *.* to 'test'@"%" identified by "test666 with g ...
- devstack with neutron 参考文献
http://networkstatic.net/installing-openstack-ml2-neutron-plugin-devstack-fedora/ https://wiki.opens ...
- 腾讯云分布式高可靠消息队列CMQ架构
版权声明:本文由张浩原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/126 来源:腾云阁 https://www.qclou ...
- apply的调用 http://bbs.51js.com/thread-82017-1-3.html
applay 这个是函数Function的方法为什么Array 也能调用这个函数?比如 function a (){ var kk = []; kk = ...
- Android反编译技术总结
一.Apk反编译工具及其使用方法 1.原理 学习反编译之前,建议先学习一下Apk打包的过程,明白打包完成后的Apk里面都有什么文件,各种文件都是怎么生成的. 这里有两篇AndroidWeekly中推荐 ...
- mysql优化之explain备忘笔记
今天使用explain来查看sql执行情况的时候发现有的东西忘掉了,故作此篇文章来强化此知识点的记忆. 1.explain作用 exlain 执行结果显示了mysql 存储引擎如何使用索引来处理sel ...
- 【git】------git的基本介绍及linux的基本命令------【巷子】
001.git简介 git是一款开源的分布式版本控制工具 在世界上所有的分布式版本控制工具中,git是最快.最简单.最流行的 git的起源 作者是Linux之父:Linus Benedict Torv ...
- html5新属性contenteditable 对于那些不可编辑的标签,现在都可以编辑了
contenteditable = true 表示该html标签的内容可以编辑,对于那些不可编辑的标签,现在都可以编辑了.
- [转]CentOS 6.4下Squid代理服务器的安装与配置
一.简介 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用 ...