C++ Primer(第五版) 第六章练习答案
目录
- C++ Primer(第五版) 第六章练习答案
-
-
- 6.1
- 6.2
- 6.3
- 6.4
- 6.5
- 6.6
- 6.7
- 6.8
- 6.9cc
- 6.9h
- 6.9cpp
- 6.10
- 6.11
- 6.12
- 6.13
- 6.14
- 6.15
- 6.16
- 6.17
- 6.18
- 6.19
- 6.20
- 6.21
- 6.22
- 6.23
- 6.24
- 6.25
- 6.26
- 6.27
- 6.28
- 6.29
- 6.30
- 6.31
- 6.32
- 6.33
- 6.34
- 6.35
- 6.36
- 6.37
- 6.38
- 6.39
- 6.40
- 6.41
- 6.42
- 6.43
- 6.44
- 6.45
- 6.46
- 6.47
- 6.48
- 6.49
- 6.50
- 6.51
- 6.52
- 6.53
- 6.54
- 6.55
- 6.56
6.1
/**
* 形参出现在函数定义中
* 实参出现在主调函数中
*/
6.2
/**
* a
* 返回类型不匹配
* int 改为 string
*
* b
* 没有返回类型 加 void
*
* c
* 形参同名 v1 改 v2
*
* d
* 函数体没有花括号 { }
*/
6.3
#include <iostream>
using std::cout; using std::endl;
int fact(int val)
{
int ret = 1;
while (val)
ret *= val--;
return ret;
}
int main()
{
cout << fact(5) << endl;
return 0;
}
6.4
#include <iostream>
using std::cout; using std::endl; using std::cin;
int fact()
{
int val;
int ret = 1;
cin >> val;
if (val > 0)
while (val)
ret *= val--;
else if (val < 0)
while (val)
ret *= val++;
else
return 0;
return ret;
}
int main()
{
cout << fact() << endl;
return 0;
}
6.5
#include <iostream>
using std::cout; using std::endl; using std::cin;
int absolute(int x)
{
if (x >= 0)
return x;
return -x;
}
int main()
{
int x;
while (cin >> x)
cout << absolute(x) << endl;
return 0;
}
6.6
// 形参 必须有初始值
int func(int x)
{
// 自动变量 可以没有初始值, 执行默认初始化
int y;
// 局部静态变量 可以没有初始值, 执行值初始化, 不会被销毁
static int z;
return ++z;
}
6.7
#include <stdio.h>
int func() { static int x; return x++; }
int main()
{
for (int i = 0; i < 10; ++i)
printf("%d\n", func());
return 0;
}
6.8
int fact(int val);
int fact();
int absolute(int x)
6.9cc
#include "6_9.h"
int func()
{
static int x;
return ++x;
}
6.9h
#ifndef _6_9_h
#define _6_9_h
int func();
#endif
6.9cpp
#include <iostream>
#include "6_9.h"
int main()
{
std::cout << func() << std::endl;
return 0;
}
6.10
#include <iostream>
using namespace std;
// 初始化一个非引用的变量时, 初始值被拷贝给变量
void func(int *x, int *y);
int main()
{
int n = 10, m = 20;
func(&n, &m);
cout << n << " " << m << endl;
return 0;
}
void func(int *x, int *y)
{
int p = *x;
*x = *y;
*y = p;
}
6.11
#include <iostream>
using namespace std;
void reset(int &x);
int main()
{
int x = 30;
reset(x);
cout << x << endl;
return 0;
}
void reset(int &x)
{
x = 20;
}
6.12
#include <iostream>
using namespace std;
void func(int &x, int &y);
/**
* 引用易于使用, 不用 解引用 取地址
*/
int main()
{
int n = 10, m = 20;
func(n, m);
cout << n << " " << m << endl;
return 0;
}
void func(int &x, int &y)
{
int p = x;
x = y;
y = p;
}
6.13
/**
* 传值
* 传引用
*/
6.14
/**
* 需要返回多个值 不想拷贝大的类类型
* 需要改变形参的值, 不需要改变实参的值
*/
6.15
/**
* const string &s 避免拷贝
* char c 不想改变实参
* string::size_type &occurs 需要改变实参
*
* s 不需要改变实参, occurs 需要改变实参
*
* char 比较 小
*
* 不能传常量 s
*
* 会报错
*/
6.16
// 不能传常量, 加 const
bool is_empty(const string &s) { return s.empty(); }
6.17
#include <iostream>
#include <string>
using namespace std;
// 小写 改 大写
void toupper(string &s);
// 有大写 返回 真
bool isupper(const string &s);
int main()
{
string s("AZng XIUU");
if (isupper(s))
toupper(s);
cout << s << endl;
return 0;
}
void toupper(string &s)
{
for (auto &c : s)
if (c >= 'A' && c <= 'Z')
c += 32;
}
bool isupper(const string &s)
{
for (const auto &c : s)
if (c >= 'A' && c <= 'Z')
return true;
return false;
}
6.18
bool compare(const matrix&, const matrix&);
vector<int>::iterator change_val(int, vector<int>::iterator);
6.19
/**
* a
* 形参个数不匹配
*
* b
* 合法 常量引用传递 值传递
*
* c
* 合法 值传递
*
* d
* vec 不是 vector<int> 时 违法
*/
6.20
/**
* 不需要改变实参的值时
* 常量不能传递
*/
6.21
int func(int x, const int *y)
{
if (x > *y)
return x;
return *y;
}
6.22
#include <iostream>
using namespace std;
void func(int **x, int **y)
{
int *p = *x;
*x = *y;
*y = p;
}
void func(int *&x, int *&y)
{
int *p = x;
x = y;
y = p;
}
int main()
{
int n = 10, m = 20, *x = &n, *y = &m;
cout << x << " " << y << endl;
func(x, y);
cout << x << " " << y << endl;
return 0;
}
6.23
#include <iostream>
using namespace std;
void print(int x)
{
cout << x << endl;
}
void print(int *arr, size_t size)
{
for (size_t i = 0; i != size; ++i)
cout << arr[i] << endl;
}
int main()
{
int i = 0, j[] = {0, 1, 2, 3};
print(i);
print(j, sizeof(j) / sizeof(int));
return 0;
}
6.24
// 不一定是 10 个 元素
void print(const int ia[10])
{
for (size_t i = 0; i != 10; ++i)
cout << ia[i] << endl;
}
void print(const int ia[], size_t size)
{
for (decltype(size) i = 0; i != size; ++i)
cout << ia[i] << endl;
}
6.25
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
if (argc > 1)
{
string s;
for (int i = 1; i < argc; ++i)
s += argv[i];
cout << s << endl;
}
return 0;
}
6.26
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
if (argc > 1)
for (int i = 1; i < argc; ++i)
cout << argv[i] << endl;
return 0;
}
6.27
#include <iostream>
#include <string>
#include <initializer_list>
using namespace std;
int sum(const initializer_list<int> &il);
int main(int argc, char *argv[])
{
initializer_list<int> il{1, 2, 3, 4, 5};
cout << sum({1, 2, 3, 4, 5, 6, 7, 8, 9}) << endl;
return 0;
}
int sum(const initializer_list<int> &il)
{
int sum = 0;
for (auto &i : il)
sum += i;
return sum;
}
6.28
/**
* string
*/
6.29
/**
* 应该
* 不用改变值
*/
6.30
#include <iostream>
#include <string>
#include <initializer_list>
using namespace std;
bool str_subrange(const string &str1, const string &str2);
int main()
{
return 0;
}
bool str_subrange(const string &str1, const string &str2)
{
if (str1.size() == str2.size())
return str1 == str2;
auto size = (str1.size() < str2.size()) ? str1.size() : str2.size();
for (decltype(size) i = 0; i != size; ++i)
{
if (str1[i] != str2[i])
return;
}
}
6.31
/**
* 返回局部变量 都无效
*/
6.32
/**
* 合法
*/
6.33
#include <iostream>
#include <vector>
using namespace std;
void putvec(const vector<int> &vec, vector<int>::size_type size);
int main()
{
vector<int> vec{1, 2, 3, 4, 5};
putvec(vec, vec.size());
return 0;
}
void putvec(const vector<int> &vec, vector<int>::size_type size)
{
if (size != 1)
putvec(vec, size - 1);
cout << vec[size - 1] << endl;
}
6.34
/**
* 结果是 0
*/
6.35
/**
* 是赋值 影响第二个 val
* val-- == val = val - 1;
*/
6.36
#include <string>
using std::string;
string (&func())[10];
6.37
#include <string>
using std::string;
string (&func1())[10];
using arrS = string[10];
arrS &func2();
auto func3() -> string(&)[10];
int arr[10];
decltype(arr) &func4();
6.38
decltype(odd) &arrPtr(int i)
6.39
/**
* a 二义性调用 忽略 顶层 const
* b 二义性调用 忽略 返回 类型
* c 正确
*/
6.40
/**
* b 错误 默认实参靠右
*/
6.41
/**
* a 调用非法 少参数
* c 调用于初衷不符 char 转换成 int
*/
6.42
#include <iostream>
#include <string>
using namespace std;
string make_plural(size_t ctr, const string &word, const string &ending = "s");
int main()
{
cout << make_plural(1, "success") << " "
<< make_plural(2, "failure") << endl;
return 0;
}
string make_plural(size_t ctr, const string &word, const string &ending)
{
return (ctr > 1) ? word + ending : word;
}
6.43
/**
* 两个都放头文件
* 内联函数 和 函数原型 放在头文件
*/
6.44
inline
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
6.45
/**
* 定义处 加 inline
*/
6.46
/**
* 不行
* string.size() 不是 constexpr 函数
*/
6.47
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
void putvec(const vector<int> &vec, vector<int>::size_type size);
int main()
{
vector<int> vec{1, 2, 3, 4, 5};
putvec(vec, vec.size());
return 0;
}
void putvec(const vector<int> &vec, vector<int>::size_type size)
{
#ifdef DEBUG
cout << "size: "<< size << endl;
#endif
if (size != 1)
putvec(vec, size - 1);
cout << vec[size - 1] << endl;
}
/**
* g++ -D DEBUG 6_47.cpp
* ./a
*/
6.48
/**
* 循环 读入 结尾符 或 sought 为止
* 然后判断 cin 是否为 真, 读入结尾符为 假
* 为 假 输出 提示
* 合理
*/
6.49
/**
* 作用域中的同名函数
* 形参与实参数量相同且类型相同或能转换
*/
6.50
/**
* a 二义性调用
* b void f(int);
* c void f(int, int)
* d void f(double, double = 3.14);
*/
6.51
#include <iostream>
using namespace std;
void f() { puts("void f()"); }
void f(int) { puts("void f(int)"); }
void f(int, int) { puts("void f(int, int)"); }
void f(double, double = 3.14) { puts("void f(double, double = 3.14)"); }
int main()
{
f();
f(true);
f(1, 'a');
f(3.34);
return 0;
}
6.52
/**
* 类型提升
* 类型转换
*/
6.53
/**
* a 精确匹配常量引用
* b 精确匹配常量指针
* c 顶层 const 被忽略 二义性调用
*/
6.54
#include <iostream>
#include <vector>
using namespace std;
int func(int, int) { static int i; cout << ++i << endl; return 0; }
int main()
{
vector<decltype(func)*> vec;
return 0;
}
6.55
#include <iostream>
#include <vector>
using namespace std;
int func1(int a, int b) { return a + b; }
int func2(int a, int b) { return a - b; }
int func3(int a, int b) { return a * b; }
int func4(int a, int b) { return a / b; }
int func5(int a, int b) { return a % b; }
int main()
{
vector<int(*)(int, int)> vec{func1, func2, func3, func4, func5};
return 0;
}
6.56
#include <iostream>
#include <vector>
using namespace std;
int func1(int a, int b) { return a + b; }
int func2(int a, int b) { return a - b; }
int func3(int a, int b) { return a * b; }
int func4(int a, int b) { return a / b; }
int func5(int a, int b) { return a % b; }
int main()
{
vector<int(*)(int, int)> vec{func1, func2, func3, func4, func5};
for (int i = 0; i < vec.size(); ++i)
cout << vec[i](10, 10) << endl;
return 0;
}