运行平台:ubuntu 12.04/GCC 4.8.0

第二章:基本内置类型

1.decltype类型指示符

当我们从表达式的类型来推断要定义的类型时,可以使用decltype()来解析;decltype与auto不同,decltype应用于变量,返回该变量的类型。

	string s("Hello World!!!");
// punct_cnt has the same type that s.size returns
decltype(s.size()) punct_cnt = 0;

2.转义序列

\xxx 八进制

\x(xxx) 十六进制

unsigned char c = -1;   // assuming 8-bit chars, c has value 255
i = c; // the character with value 255 is an unprintable character
// assigns value of c (i.e., 255) to an int
std::cout << i << std::endl; // prints 255
	unsigned u = 10;
int i = -42;
std::cout << i + i << std::endl; // prints -84
std::cout << u + i << std::endl; // if 32-bit ints, prints 4294967264

3.getline()的使用

string line;

	// read input a line at a time until end-of-file
while (getline(cin, line))
cout << line << endl;

4.str.size()返回为string::size_type类型,这是标准库与机器无关的特性之一。

如下面这个例子,输入大小在0-15之间的数字,求其对应的十六进制符号:

const string hexdigits = "0123456789ABCDEF";  // possible hex digits

	cout << "Enter a series of numbers between 0 and 15"
<< " separated by spaces. Hit ENTER when finished: "
<< endl;
string result; // will hold the resulting hexify'd string string::size_type n; // hold numbers from the input
while (cin >> n)
if (n < hexdigits.size()) // ignore invalid input
result += hexdigits[n]; // fetch the indicated hex digit cout << "Your hex number is: " << result << endl;

而与之相关的则是size_t类型,size_t是标准C库中定义的,应为unsigned int,在64位系统中为 long unsigned int。

#include <cstddef>
using std::size_t; constexpr size_t rowCnt = 3, colCnt = 4;
int ia[rowCnt][colCnt]; // 12 uninitialized elements // for each row
for (size_t i = 0; i != rowCnt; ++i) {
// for each column within the row
for (size_t j = 0; j != colCnt; ++j) {
// assign the element's positional index as its value
ia[i][j] = i * colCnt + j;
}
}

第四章:表达式

1.值的溢出

思考下面运算的结果:

short short_value = 32767; // max value if shorts are 16 bits

	short_value += 1; // this calculation overflows
cout << "short_value: " << short_value << endl;

第六章:函数

1.返回数组指针的函数,这三种表达方式,留意:

int *p = elemPtr(6);         // p points to an int
int (*arrP)[5] = arrPtr(5); // arrP points to an array of five ints
int (&arrR)[5] = arrRef(4); // arrR refers to an array of five ints / two arrays
int odd[] = {1,3,5,7,9};
int even[] = {0,2,4,6,8}; // function that returns a pointer to an int in one of these arrays
int *elemPtr(int i)
{
// returns a pointer to the first element in one of these arrays
return (i % 2) ? odd : even;
} // returns a pointer to an array of five int elements
decltype(odd) *arrPtr(int i)
{
return (i % 2) ? &odd : &even; // returns a pointer to the array
} // returns a reference to an array of five int elements
int (&arrRef(int i))[5]
{
return (i % 2) ? odd : even;
}

2.含有可变参数的函数

在C11中提供了两种方法:如果参数类型相同,可以传递一个名为initializer_list的标准库类型;如果类型不同,可以使用可变参数模版,见16章。使用方法如下:

// overloaded version takes only a list of strings
void error_msg(initializer_list<string> il)
{
for (auto beg = il.begin(); beg != il.end(); ++beg)
cout << *beg << " " ;
cout << endl;
} string expected = "description", actual = "some other case";
initializer_list<int> li = {0,1,2,3}; // expected, actual are strings
if (expected != actual)
error_msg({"functionX", expected, actual});
else
error_msg({"functionX", "okay"});

3.函数指针

C11中尾置返回类型,如声明如下:

// use trailing return type
auto getFcn(const string&) -> string::size_type(*)(const string&, const string&);

  而函数指针的另外两种声明定义,一种是()括号解析,另一种则是借用decltype确定返回类型为函数指针类型,使用如下:

decltype(sumLength) *getFcn(const string &);

// direct definition
string::size_type (*getFcn(const string&))(const string&, const string&); // define getFcn
decltype(sumLength)*
getFcn(const string &fetch)
{
if (fetch == "sum")
return sumLength;
return largerLength;
}

《C++ Primer(第五版)》知识巩固的更多相关文章

  1. 实验楼课程管理程序-深入学习《C++ Primer第五版》实验报告&学习笔记1

    本片博客为实验楼的训练营课程深入学习<C++ Primer第五版>的实验报告和学习笔记. 原课程地址为:https://www.shiyanlou.com/courses/405# 原文出 ...

  2. C++ 11 从C++ primer第五版的学习笔记

    1. auto (page107) auto 推断会忽略const   const int ci = i, & cr = ci; auto b = ci; // b is an int (to ...

  3. C++PRIMER第五版练习题答案第一章

    C++PRIMER第五版练习题答案第一章 应该有很多小伙伴和我一样,闲来无事买了本C++的书自己啃,课后的练习题做的很揪心,这里我分享下我写的答案,希望能帮助到你,提供源码,就不跑了哈,毕竟现在是第一 ...

  4. C++Primer第五版学习笔记

    <C++ Primer>Learning Note 程序实例下载地址:http://www.informit.com/title/0321714113 第一章            开始 ...

  5. C++Primer第五版——习题答案和解析

    感谢原文博主的分享:https://blog.csdn.net/misayaaaaa/article/details/53786215 新手入门必看的书.知识是一个系统化并且相互关联的体系,零散的东西 ...

  6. C++学习书籍推荐《C++ Primer 第五版 (英文)》下载

    百度云及其他网盘下载地址:点我 编辑推荐 <C++ Primer(英文版)(第5版)>是全球最畅销的C++图书.这本久负盛名的C++经典教程,时隔八年之久,终迎来的重大升级.除令全球无数程 ...

  7. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  8. C++ primer第五版随笔--2015年1月6日

    记录自己看这本书时的一些内容. 一.引用(reference) 引用为对象起了另外一个名字.例如: int ival=1024: int &relVal1=ival;//对,注意尽量不要用这方 ...

  9. C++Primer第五版——习题答案详解(一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

  10. C++Primer第五版——习题答案详解(二)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

随机推荐

  1. CALayer, CoreGraphics与CABasicAnimation介绍

    今天我们来看一下CALayer.CoreGraphics和CABasicAnimation.这些东西在处理界面绘制.动画效果上非常有用. 本篇博文就讲介绍CALayer的基本概念,使用CoreGrap ...

  2. kafka学习默认端口号9092

    一 Kafka 概述1.1 Kafka 是什么在流式计算中,Kafka 一般用来缓存数据,Storm 通过消费 Kafka 的数据进行计算.1)Apache Kafka 是一个开源消息系统(微信公众号 ...

  3. Gym 100989L (DFS)

    AbdelKader enjoys math. He feels very frustrated whenever he sees an incorrect equation and so he tr ...

  4. 微信小程序实例代码

    http://blog.csdn.net/zuoliangzhu/article/details/53862576#t1 项目结构 └─ empty-folder/ ················· ...

  5. Arcgis Android 坐标转换

    http://spatialreference.org/首先,在上面的网站查出现有的坐标srid,然后查出目标Srid. 参考api 示例代码 Point point = new Point(120. ...

  6. duilib入门简明教程 -- 完整的自绘标题栏(8)

       看了前面那么多教程,相信对duilib已有基本映像了,我们就快马加鞭,做出一个完整的自绘标题栏吧~     看到下面这个效果图,小伙伴们是不是有点惊呆了呢~O(∩_∩)O~       duil ...

  7. 网站运维之 使用IIS日志分析器1.03.exe进行IIS服务器日志分析

    引言 对于网站运维是一个比较要细心有耐心的工作,当一个网站从开发到上线后,后期的维护也很关键,特别是对于引流的网站来说更是至关重要. 对于网站运维的内容大致可以分为: SEO流量监控方面:风险防控:访 ...

  8. Page.FindControl(string id) 与母版页结合后发现的一个问题

    MSDN上解释Page.FindControl(string id)方法用于查找指定ID的控件.例如: <asp:TextBox id="Email" runat=" ...

  9. Robot Framework连接MySQL数据库

    注:内容来自网络,整理之如下 Robot Framework连接mysql数据库需要: 1.安装databaselibrary.pymysql 通过cmd命令执行:pip install robotf ...

  10. spring-第二章-AOP

    一,回顾 1.控制反转(IOC) 以前创建对象,由我们自己决定,现在我们把管理对象的声明周期权力交给spring; 2.依赖注入(DI) A对象需要B对象的支持,spring就把B注入给A,那么A就拥 ...