打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++ Primer第六章函数习题
C++ Primer第六章函数习题
6.1
实参就是函数调用时传递的参数,形参是函数声明和定义时的参数
6.2
a)函数返回值是int类型返回了string类型并且返回了局部变量
b)没有定义函数的返回类型
c)函数花括号啥一个
d)没有花括号将函数体括起来
6.3
int fact(int val)
{
int ret = 1;
while (val > 1)
{
ret *= val--;
}
return ret;
}
6.4
int fact()
{
int ret = 0,num = 0;
cout << "Please enter a number:" << endl;
cin >> num;
while (num > 1)
{
ret *= num--;
}
return ret;
}
6.5
int abs(int val)
{
if(val < 0)
val = -val;
return val;
}
6.6
形参是一种自动对象函数开始的时候为形参申请存储空间,因为形参定义在函数体作用域之内所以一旦函数终止,形参也就被销毁。局部对象如果是非静态的,那么其生存周期从被创建到函数体结束.
int fact(int n)
{
int i = 2;
static int cnt = 0;
++cnt;
cout << cnt*2 << '';
return n>0?n:(-1)*n;
}
6.7
int Counter()
{
static int cnt = 0;
return cnt++;
}
6.8
#ifndef chapter6_h
#define chapter9_h
int fact();
#endif chapter6_h
6.9
//chapter6.cpp
#include <iostream>
#include <string>
#include "chapter6.h"
using namespace std;
int fact()
{
static int cnt = 0;
return cnt++;
}
6.10
void swap(int *p1,int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
6.11
void reset(int &i) { i = 0;}
6.12
void swap(int &val1,int &val2)
{
int temp = val1;
val1  = val2;
val2 = temp;
}
6.13
第一种是传值调用不会修改原来的值,第二种是传址调用会修改实参的值。
6.14
希望在函数调用过程去修改实参的值,使用引用和指针如果不希望改变实参则传值
6.15
常量引用可以避免参数发生拷贝,同时可以通过形参去修改实参的值。
6.16
参数可以用const string &s改成常引用
6.17
一个是const引用,一个是引用
6.18
bool compare(Matrix &m1,Matrix &m2);
vector<int>::iterator change_val(int,vactor<int>::iterator)
6.19
a)不合法参数个数不匹配
b)合法
c)合法将int类型转换成double
d)合法
6.20
不希望改变实参值时使用常引用。
6.21
int greater(int a,int *b)
{
return (a > *b?a:*b);
}
6.22
void swap(int **a,int **b)
{
int **temp = &a;
a = b;
b = *temp;

}
6.23略
6.24
不存在问题
输出数组元素
6.25
int main(int argc,char* argv[])
{
string str = argv[1];
str += argv[2];
}
6.26
int main(int argc,char *argv[])
{
for (int i = 0; i < argc; i ++)
{
cout << arvc[i] << endl;
}
}
6.27
int sum(const initializer_list<int> &vals)
{
int ret = 0;
for (auto beg = vals.begin();beg != vals.end(); ++beg)
{
ret += *beg;
}
return ret;
}
6.28
string
6.29
不能.因为声明引用类型,为const不能自加了
6.30
编译不通过不是所有路径都有返回值
6.31
返回局部对象的引用会死无效的。当需要给返回的引用赋值时返回常量引用是无效的
6.32
合法
6.33
static size_t i = 0;
void print(vector<int>ivec)
{
if(ivec.size() != i)
{
cout << ivec[i++] << endl;
print(ivec);
}
}
6.34
val是负数时就会发生无限递归,栈溢出
6.35
用val--无法递归
6.36
string (&func(string s))[10]
6.37
typedef string sPtr[10];
sPtr &func(string s);
string str[10];
decltype(str) &fun(string s);
auto fun(string s)->string(&)[10];
6.38
int odd[] = {1,3,5,7,9};
int even[] = {0,2,4,6,8};
decltype(odd)& arrPtr(int i)
{
return (i % 2)?odd:even;
}
6.39
a)顶层const非法
b)非法
c)合法重载
6.40
b)默认参数应该放在后面
6.41
ac错误
6.42
string make_plural(size_t ctr,const string &word = "success",const string &ending = "es")
{
return (ctr > 1)?word+ending:word;
}
6.43
a)放在头文件
6.44
inline const string &shorterString(const string &s1,onst string &s2)
6.45
需要就可以写成内联函数,没必要非套把所有函数都写成内联函数。内联机制用于优化规模较小,流程直接,频繁调用的函数
6.46
可以
constexpr const string &shortString(const string&s1,const string &s2)
6.47
6.48
不合理因为结束输入也会出错
6.49
候选函数 一是与调用函数重名,二是声明在调用点可见
可行函数 形参数量相同,二是实参类型对应形参类型相同或能转换
6.50
a)二义性 b调用void f(int) c调用void f(int,int)
d)调用void f(double,double)
6.51
6.52
将char类型转换成int
将double类型转成int
6.53
a)重复定义
b)
6.54
a,b函数重载
c有影响,形参是按值,不能通过形参是否是const来重载函数
6.54
int fun(int,int);
typedef int(*pFun)(int,int);
vector<pFunc>pFuncVec;
6.55
int add(int a,int b)
{
return a + b;
}
int sub(int a,int b)
{
return a - b;
}
int div(int a,int b)
{
if(b == 0) 
return 0;
return a/b;
}
int multi(int a,int b)
{
return a * b;
}
pFuncVec.push_back(add);
pFuncVec.push_back(sub);
pFuncVec.push_back(div);
pFuncVec.push_back(multi);
6.56
运行上面的代码



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
函数返回值
C语言字符串操作总结大全(超详细)
fzyzojP3372 -- [校内训练20171124]博弈问题
spfa + slf优化
C++写的一个链表类
02、常量引用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服