-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
311 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)(来源于书籍相关网站) |