Skip to content

Commit

Permalink
16.42-16.52
Browse files Browse the repository at this point in the history
  • Loading branch information
jzplp committed Aug 9, 2019
1 parent 860bdbf commit ff19589
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 20 deletions.
24 changes: 24 additions & 0 deletions Chapter-16/16.47.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<iostream>
#include<utility>

template <typename F, typename T1, typename T2>
int filp(F f, T1 && t1, T2 &&t2)
{
f(std::forward<T2>(t2), std::forward<T1>(t1));
}

void print1(int & v1, int && v2)
{
std::cout << v1 << " " << v2 << std::endl;
}

int main()
{
int i = 1;
const int j = 2;
filp(print1, 1, i);
filp(print1, 2, i);

return 0;
}

55 changes: 55 additions & 0 deletions Chapter-16/16.48.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<iostream>
#include<string>
#include<sstream>

template <typename T> std::string debug_rep(const T &t);
template <typename T> std::string debug_rep(T * p);
std::string debug_rep(const std::string &s);
std::string debug_rep(char *p);
std::string debug_rep(const char *p);

template <typename T>
std::string debug_rep(const T &t)
{
std::ostringstream ret;
ret << t;
return ret.str();
}

template <typename T>
std::string debug_rep(T * p)
{
std::ostringstream ret;
ret << "pointer: " << p;
if(p)
ret << " " << debug_rep(*p);
else
ret << " null pointer";
return ret.str();
}

std::string debug_rep(const std::string &s)
{
return '"' + s + '"';
}

std::string debug_rep(char *p)
{
return debug_rep(std::string(p));
}
std::string debug_rep(const char *p)
{
return debug_rep(std::string(p));
}

int main()
{
std::string s("hi");
std::string * sp = &s;
std::cout << debug_rep(s) << std::endl;
std::cout << debug_rep(&s) << std::endl;
std::cout << debug_rep(sp) << std::endl;
std::cout << debug_rep("hi world!") << std::endl;
return 0;
}

36 changes: 36 additions & 0 deletions Chapter-16/16.50.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<iostream>
#include<string>

template <typename T>
void f(T t)
{
std::cout << "void f(T t)" << std::endl;
}

template <typename T>
void f(const T * t)
{
std::cout << "void f(const T * t)" << std::endl;
}

template <typename T>
void g(T t)
{
std::cout << "void g(T t)" << std::endl;
}

template <typename T>
void g(T * t)
{
std::cout << "void g(T * t)" << std::endl;
}

int main()
{
int i = 42, *p = &i;
const int ci = 0, *p2 = &ci;
g(42); g(p); g(ci); g(p2);
f(42); f(p); f(ci); f(p2);
return 0;
}

22 changes: 22 additions & 0 deletions Chapter-16/16.52.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>
#include<string>

template <typename T, typename ... Args>
void foo(const T &t, const Args & ... rest)
{
std::cout << sizeof...(Args) << std::endl;
std::cout << sizeof...(rest) << std::endl;
}

int main()
{
int i = 0;
double d = 3.14;
std::string s = "how now brown cow";
foo(i, s, 42, d);
foo(i, 42, "hi");
foo(d, s);
foo("hi");
return 0;
}

123 changes: 123 additions & 0 deletions Chapter-16/chapter-16-answer.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,127 @@ auto sum(T1 a, T2 b) -> decltype(a + b)
```

* **练习16.42**
```
(a) T : int & val : int &
(b) T : const int & val : const int &
(c) T : int val : int &&
```

* **练习16.43**
```
T : int & val : int &
```

* **练习16.44**
```
template<typename T> void g(T val);
(a) T : int val : int
(b) T : int val : int
(c) T : int val : int
template<typename T> void g(const T & val);
(a) T : int val : const int &
(b) T : int val : const int &
(c) T : int val : const int &
```

* **练习16.45**
如果用一个42调用g,则42是字面值常量,是右值,因此T为int,val为int && 类型。
如果用一个int类型变量调用g,因为变量是左值,将一个左值传递给函数的右值引用参数,编译器会推断模板T的类型为int & ,这样val的类型就是 int & &&,折叠后就是 int &

* **练习16.46**
这段代码是要把元素从旧地址移动到新地址,虽然是左值,但是我们确定移动是安全的。
首先elem是string * 类型,*elem++是令elem指向下一个位置,同时返回当前位置对象的左值引用。
然后用std::move函数令左值类型转换为右值引用,再传参给alloc.construct函数,令其利用这个右值移动到新的内存中。

* **练习16.47**
[16.47 程序代码](16.47.cpp)

* **练习16.48**
debug_rep模板重载函数 书上的版本
[16.48 程序代码](16.48.cpp)

* **练习16.49**
```
g(42)
候选函数:
void g<int>(int);
最后选择:
void g<int>(int);
g(p)
候选函数:
void g<int *>(int *);
void g<int>(int *);
最后选择:
void g<int>(int *);
g(ci)
候选函数:
void g<int>(int);
最后选择:
void g<int>(int);
g(p2)
候选函数:
void g<const int *>(const int *);
void g<const int>(const int *);
最后选择:
void g<const int>(const int *);
f(42)
候选函数:
void f<int>(int);
最后选择:
void f<int>(int);
f(p)
候选函数:
void f<int *>(int *);
void f<int>(const int *);
最后选择:
void f<int *>(int *);
f(ci)
候选函数:
void f<int>(int);
最后选择:
void f<int>(int);
f(p2)
候选函数:
void f<const int *>(const int *);
void f<int>(const int *);
最后选择:
void f<int>(const int *);
```

* **练习16.50**
[16.50 程序代码](16.50.cpp)
程序输出的结果是:
```
void g(T t)
void g(T * t)
void g(T t)
void g(T * t)
void f(T t)
void f(T t)
void f(T t)
void f(const T * t)
```
结果相同

* **练习16.51**
猜测结果为:
```
foo(i, s, 42, d);
sizeof...(Args) 为 3
sizeof...(rest) 为 3
foo(i, 42, "hi");
sizeof...(Args) 为 2
sizeof...(rest) 为 2
foo(d, s);
sizeof...(Args) 为 1
sizeof...(rest) 为 1
foo("hi");
sizeof...(Args) 为 0
sizeof...(rest) 为 0
```

* **练习16.52**
[16.52 程序代码](16.52.cpp)

* **练习16.53**

71 changes: 51 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
# Cpp-Primer-Answer
C++ Primer 第五版中文版 练习题答案代码(个人所做)
C++ Primer 第五版中文版 练习题答案代码

注:中文版与英文版不同的,一般按照中文版处理。

* [书上的源代码 zip压缩包](C++Primer-FiveEdition-SourceCode.zip)(来源于书籍相关网站)
* 包含全部的题目答案(正在完成)
* 包含问答题回答,编程题代码等内容
* 中文版与英文版内容不同的,一般按照中文版处理
*[jzplp](https://github.com/jzplp)独自完成

## 答案目录
* 第一章 开始
[第一章练习题答案](Chapter-1/chapter-1-answer.md)
* 第一部分 C++基础
* 第二章 变量和基本类型
[第二章 练习题答案](Chapter-2/chapter-2-answer.md)
* 第三章 字符串,向量和数组
[第三章 练习题答案](Chapter-3/chapter-3-answer.md)
* 第四章 表达式
[第四章 练习题答案](Chapter-4/chapter-4-answer.md)
* 第五章 语句
[第五章 练习题答案](Chapter-5/chapter-5-answer.md)
* 第六章 函数
[第六章 练习题答案](Chapter-6/chapter-6-answer.md)
* 第七章 类
[第七章 练习题答案](Chapter-7/chapter-7-answer.md)
* 第二部分 C++标准库
* 第八章 IO库
[第八章 练习题答案](Chapter-8/chapter-8-answer.md)
* 第九章 顺序容器
[第九章 练习题答案](Chapter-9/chapter-9-answer.md)
* 第十章 泛型算法
[第十章 练习题答案](Chapter-10/chapter-10-answer.md)
* 第十一章 关联容器
[第十一章 练习题答案](Chapter-11/chapter-11-answer.md)
* 第十二章 动态内存
[第十二章 练习题答案](Chapter-12/chapter-12-answer.md)
* 第三部分 类设计者的工具
* 第十三章 拷贝控制
[第十三章 练习题答案](Chapter-13/chapter-13-answer.md)
* 第十四章 重载运算与类型转换
[第十四章 练习题答案](Chapter-14/chapter-14-answer.md)
* 第十五章 面向对象程序设计
[第十五章 练习题答案](Chapter-15/chapter-15-answer.md)
* 第十六章 模板与泛型编程
[第十六章 练习题答案](Chapter-16/chapter-16-answer.md)
* 第四部分 高级主题

## 编译环境
* Windows 10
* Visual Studio 2015
* 编译命令
```
cl /EHsc XXX1.cpp XXX2.cpp -o 1.exe
```

* [第一章 练习题答案](Chapter-1/chapter-1-answer.md)
* [第二章 练习题答案](Chapter-2/chapter-2-answer.md)
* [第三章 练习题答案](Chapter-3/chapter-3-answer.md)
* [第四章 练习题答案](Chapter-4/chapter-4-answer.md)
* [第五章 练习题答案](Chapter-5/chapter-5-answer.md)
* [第六章 练习题答案](Chapter-6/chapter-6-answer.md)
* [第七章 练习题答案](Chapter-7/chapter-7-answer.md)
* [第八章 练习题答案](Chapter-8/chapter-8-answer.md)
* [第九章 练习题答案](Chapter-9/chapter-9-answer.md)
* [第十章 练习题答案](Chapter-10/chapter-10-answer.md)
* [第十一章 练习题答案](Chapter-11/chapter-11-answer.md)
* [第十二章 练习题答案](Chapter-12/chapter-12-answer.md)
* [第十三章 练习题答案](Chapter-13/chapter-13-answer.md)
* [第十四章 练习题答案](Chapter-14/chapter-14-answer.md)
* [第十五章 练习题答案](Chapter-15/chapter-15-answer.md)
* [第十六章 练习题答案](Chapter-16/chapter-16-answer.md)
## 其他内容
* [书上的源代码 zip压缩包](C++Primer-FiveEdition-SourceCode.zip)(来源于书籍相关网站)

0 comments on commit ff19589

Please sign in to comment.