C++Prime 第六章 后36题

C++Prime 第六章 后36题

练习6.31

返回对局部变量的引用无效.

常量如果是在函数中,那么其实它也是局部的,同上.

练习6.32

合法滴,将指定数组元素修改为0 - 10.

练习6.33

#include <iostream>
#include <vector>
using namespace std;

void print(vector<int> vi,int n)
{
	if (n == vi.size())
		return;
	cout << vi[n] << " ";
	print(vi, n + 1);
}

int main()
{
	vector<int> vi{ 1,2,3,4,5,6,7,8 };
	print(vi, 0);
	return 0;
}

练习6.34

需要再对传入参数检查大于0,否则<=0时会报错

练习6.35

可以是--val,不能是val--.

练习6.36

string (&func(string(& arr)[10]))[10];

练习6.37

//普通方法
string (&func(string(& arr)[10]))[10];

//类型别名
typedef string (&arrT)[10];
//using arrT = string &;  //使用using的类型别名
arrT func(arrT a);

//后置返回类型
auto func(string(&arr)[10])->string(&)[10];

//decltype关键字
string (&arr)[10];
decltype(arr) func(decltype(arr));

练习6.38

decltype(odd)& arrPtr(int i)

练习6.39

(a)重复声明,无意义.

(b)非法.无法区别仅按返回类型区分的函数

练习6.40

(b)错了.默认参数必须从右至左赋值.

练习6.41

(a)非法,ht没初值

(c)违背了初衷

练习6.42

#include <iostream>
using namespace std;

string make_plural(size_t ctr, const string& word = "s", const string& ending = "")
{
	return (ctr > 1) ? word + ending : word;
}
int main()
{
	cout << make_plural(1, "success")<<endl;
	cout << make_plural(2, "success", "es") << endl;
	cout << make_plural(1, "failure") << endl;
	cout << make_plural(2, "faliure", "s") << endl;
	return 0;
}


练习6.43

都放在头文件中.

练习6.44

加个inline

练习6.45

代码量少,不递归,频繁调用的函数都可以考虑成为内联函数

练习6.46

不能改写成constexpr函数.

constexpr函数有两个必要知识点,第一,返回值和所有形参的类型都是字面值类型,函数体中必须有且只有一条return语句.

练习6.47

#include <iostream>
#include <vector>
using namespace std;

void print(vector<int> vi, int n)
{
	if (n == vi.size())
		return;
	cout << vi[n] << " "
		    << "此时vector对象的大小为: " << vi.size() << endl;;
	print(vi, n + 1);
}

int main()
{
	vector<int> vi{ 1,2,3,4,5,6,7,8 };
	print(vi, 0);
	return 0;
}

练习6.48

不合理,最好检查 s == sought.比如 assert(s == sought);

练习6.49

候选函数:与被调用的函数同名,其声明再调用点可见.

可行函数:其形参与本次调用的实参数量相等,实参的类型与对应的形参类型相同.

练习6.50

(a) 没有匹配上的

(b) void f(int)

(c) void f(int,int)

(d) void f(double,double)

练习6.51

#include <iostream>
#include <vector>
using namespace std;

void f()
{
	cout << "我是无参数版本\n";
}
void f(int)
{
	cout << "我是一个参数的int版本\n";
}
void f(int, int)
{
	cout << "我是两个int的版本\n";
}
void f(double, double)
{
	cout << "我是两个double的版本\n";
}

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

练习6.52

(a)  3  

(b) 4 

练习6.53

不会.

练习6.54

typedef int (*p)(int,int);

vector<p> v;

练习6.55 练习6.56

#include <iostream>
#include <vector>
using namespace std;

int f1(int a, int b) { return a + b; }
int f2(int a, int b) { return a - b; }
int f3(int a, int b) { return a * b; }
int f4(int a, int b) { return a / b; }  //b不为0

int main()
{
	typedef int (*p)(int, int);
	vector<p> v{ f1,f2,f3,f4 };
	for (const auto& x : v)
		cout << x(1, 2) << endl;

	return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值