C++ Primer(5e)第6章习题

6.1

实参是形参的初始值。

6.2

(a) 返回类型不一致
int f() {
	string s;
	//...
	return s;
}
修改如下:
string f() {
	string s;
	//...
	return s;
}

(b)要声明返回类型
f2(int i) { /*...*/ }     
修改: void f2(int i) { /*...*/}

(c)一个函数中不能有相同的形参
int calc(int v1, int v1) { /*...*/ }
修改: int cal(int v1, int v2) { /*...*/ }

(d)缺少语句块的括号
double square(double x)  return x * x
修改: double square(double x) { return x * x } 

6.3

#include<iostream>

using namespace std;

int fact(int val)
{
	int ret = 1;
	while (val > 1)
		ret *= val--;
	return ret;
}

int main()
{
	int j;
	j = fact(5);
	cout << j << endl;
	return 0;
}

6.4

#include<iostream>

using namespace std;

int fact()
{
	int i = 0, j = 1;
	cout << "Enter a number: ";
	cin >> i;
	while (i > 1)
		j *= i--;
	return j;
}

int main()
{
	int i;
	i = fact();
	cout << "该数的阶乘: " << i << endl;
	return 0;
}

6.5

unsigned abs(int val)
{
	if (val < 0)
		return -val;
	else
		return val;
}

6.6

形参和函数体内部定义的变量统称为局部变量;局部静态变量在程序的执行路径第一次经过对象定义语句时初始化,并且直到程序终止才销毁。

int fact(int val)
{
	static int ctr = 0;
	int r = 1;
	while (val > 1)
	{
		ctr++;
		r = r * (ctr + r);
		
	}
	return r;
}

6.7

size_t fact()
{
	static size_t ctr = 0;
	return ++ctr;
}

6.8

在这里插入图片描述

6.9

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6.10

#include<iostream>

using namespace std;

void swap(int *p1, int *p2)
{
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}

int main()
{
	int i = 1, j = 2;
	int *p1 = &i, *p2 = &j;
	cout << *p1 << " " << *p2 << endl;
	swap(p1, p2);
	cout << *p1 << " " << *p2 << endl;
	return 0;
}

6.11

#include<iostream>
using namespace std;

void reset(int &i)
{
	i = 0;
}

int main()
{
	int i = 42;
	reset(i);
	cout << "i = " << i << endl;
	return 0;
}

6.12

#include<iostream>
using namespace std;

void swap(int &i, int &j)
{
	int temp = i;
	i = j;
	j = temp;
}

int main()
{
	int n = 1, m = 2;
	int &i = n, &j = m;
	cout << i << " " << j << endl;
	swap(i, j);
	cout << i << " " << j << endl;
	return 0;
}

6.13

void f(T) : T是传值参数
void f(&T) : T是传引用参数

6.14

应该是引用类型:不支持拷贝操作时
不能是引用类型:当形参是常量时

6.15

因为s由const定义,故s是常量引用,occurs是普通引用;因为s和occurs用了&作为引用,故s和occurs是引用类型而c不是;如果令s是普通引用则只能将find_char函数作用于string对象;如果令occurs是常量引用则无法修改该occurs的值,无法确定字符出现的次数。

6.16

无法令字符串常量作为实参传入。

修改: bool is_empty(const string &s) { return s.empty(); }

6.17

#include<iostream>
#include<string>

using namespace std;

void Upperletter(const string s)
{
	for (decltype(s.size()) i = 0; i != s.size(); ++i)
	{
		if (s[i] >= 'A' && s[i] <= 'Z')
		{
			cout << "Yes" << endl;
			break;
		}
	}
}

void Tolower(string s)
{
	for (auto &i : s)
		i = tolower(i);
	cout << s << endl;
}

int main()
{
	const string s1 = "Hello World";
	string s2 = "Hello World";
	Upperletter(s1);
	cout << s2 << endl;
	Tolower(s2);
	return 0;
}

在这两个函数中使用的形参类型不相同,因为第一个函数不需要改变string对象的内容故使用const,而第二个函数需要改变string对象的内容。

6.18

(a)bool compare(matrix &, matrix &); 
比较两个矩阵运算
(b)vector<int>::iterator change_val(int, vector<int>::iterator);
给迭代器赋值

6.19

(a)不合法,形参只有一个
(b)不合法,不能用字符常量'a'char作为实参
(c)合法,66会转化为double类型
(d)不合法,不能用整数常量给int作为实参

6.20

当引用形参不需要修改的时候应该用常量引用;如果形参是常量引用,而我们将其设为普通引用,则无法用常量作为实参,更可能在调用时发生错误。

6.21

int max(int v, int *p)
{
	if (v > *p)
		return v;
	else
		return *p;
}

6.22

void swap(int *p1, int *p2)
{
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}

6.23

#include<iostream>
using namespace std;

void print(const int *beg, const int *end)
{
	while (beg != end)
		cout << *beg++ << endl;
}

int main()
{
	int j[2] = { 0, 1 };
	print(begin(j), end(j));
	return 0;
}

6.24

输出数组的值
在这里插入图片描述

6.25

#include<iostream>

using namespace std;

int main(int argc, char const *argv[])
{
	string s = argv[1];
	for (auto i = 2; i != argc; ++i)
		s += s[i];
	cout << s << endl;
	return 0;
}

6.26

#include<iostream>

using namespace std;

int main(int argc, char const *argv[])
{
	for (int i = 1; i != argc; ++i)
		cout << argv[i] << " ";
	return 0;
}

6.27

int Sum(initializer_list<int> num)
{
	int sum = 0;
	for (auto beg = num.begin(); beg != num.end(); ++beg)
		sum += *beg;
	return sum;
}

6.28

const int&类型

6.29

应该将循环控制变量声明成引用类型,因为initalizer_size对象中的元素永远都是常量值。

6.30

在这里插入图片描述

6.31

返回局部变量的引用无效;当引用的对象需要修改时,返回常量的引用无效。

6.32

int &get(int *arry, int index) { return arry[index]; }
int main()
{
	int ia[10];
	for (int i = 0; i != 10; ++i)
		get(ia, i) = i;
}
合法,给ia数组的元素赋值0~9

6.33

vector<int> vec(const vector<int> &num, vector<int>::iterator iter)
{
	if (iter != s.end())
	{
		cout << *iter << " ";
		return vec(s, (iter + 1));
	}
	return {};
}

6.34

val传入的值为负数时,发生溢出(负数没有阶乘)。

6.35

如果传入的值为val--,则会进入死循环。

6.36

string (*func(string s)) [10];

6.37

类型别名:using arrT = string[10];  arrT *func(string s);
尾置返回类型: auto func(string s) -> string(*)[10];
decltype类型:decltype(string[10]) *func(string s);

6.38

decltype(odd) &arrPtr(int i);

6.39

(a) int calc(int, int);
	int calc(const int, const int);
	重复声明了int calc(int, int)
	
(b) int get();
	double get();
	错误,不允许两个函数除了返回类型外其他所有的要素都相同。
	
(c) int *reset(int *);
	double *rest(double *);
	形参为double类型指针,返回double类型指针的rest函数。

6.40

(a) int ff(int a, int b = 0, int c = 0);    //正确
(b) char *init(int ht = 24, int wd, char bckgrnd);
// 错误,一旦某个形参被赋予了初始值,那么它后面的所有形参都要被赋予初始值。

6.41

char *init(int ht, int wd = 80, char bckgrnd = ' ');
(a) init();    // 不合法, 第一个形参ht未赋值
(b) init(24, 10);  // 合法
(c) init(14, '*'); // 合法,但与程序员的初衷不符。
因为第二个形参的内容是'*',而程序员的初衷是第三个形参的内容是'*'

6.42

#include<iostream>
#include<string>

using namespace std;

string make_plural(size_t ctr, const string &word,const string &ending = "s")
{
	return (ctr > 1) ? word + ending : word;
}

int main()
{
	cout << make_plural(2, "succes", "es") << endl;
	cout << make_plural(2, "failure") << endl;
	cout << make_plural(1, "success") << endl;
	cout << make_plural(1, "failure") << endl;
	return 0;
}

6.43

(a)inline bool eq(const BigInt&, const BigInt&) {...}
// 放在头文件中,因为对于某个给定的内联函数,它的多个定义必须完全一致。
(b)void putValues(int *arr, int size);
// 放在源文件中,额,原因不太清楚。。。方便直观吧

6.44

inline bool isShorter(const string &s1, const string &s2)
{
	return si.size() < s2.size();
}

6.46

不能,因为string类型不属于字面值类型。

6.48

string s;
while (cin >> s && s != sought) { } // 空函数体
assert(cin);
当不再输入s的内容或者s的内容等于sought,assert输出信息并终止程序的执行。

6.49

候选函数:调用对应的重载函数集里的函数
可行函数:从候选函数中选出能被这组实参调用的函数。

6.50

(a)调用不合法,调用具有二义性
(b)void f(int);
(c)void f(int, int);
(d)void f(double, double);

6.51

#include<iostream>

using namespace std;

void f(int n)
{
	cout << n << endl;
}

void f(int n1, int n2)
{
	cout << n1 << n2 << endl;
}

void f(double n)
{
	cout << n << endl;
}

void f(double n1, double n2)
{
	cout << n1 << n2 << endl;
}

int main()
{
	f(2.56, 42);
	f(42);
	f(42, 0);
	f(2.56, 3.14);
	return 0;
}

在这里插入图片描述
当我去掉a选项后:
在这里插入图片描述

6.52

(a) 等级3
(b) 等级4

6.53

(a)函数作用于常量引用
(b)函数作用于常量指针
(c)重复声明

6.54

int func(int , int);
vector<int (*)(int, int)> func1;

6.55 6.56

#include<iostream>
#include<vector>

using namespace std;

int func_add(int n1, int n2)   // 加法
{
	return n1 + n2;
}

int func_sub(int n1, int n2)   // 减法
{
	return n1 - n2;
}

int func_mul(int n1, int n2)   // 乘法
{
	return n1 * n2;
}

int func_div(int n1, int n2)   // 除法
{
	return n1 / n2;
}

int main()
{
	vector<int (*)(int, int)> num = { func_add, func_sub, func_mul, func_div };
	for (auto &c : num)
		cout << c(10, 5) << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值