C++Prime 第六章 前30题
练习6.1
实参和形参的区别: 实参是实际提供给函数的参数,实参是形参的初始化值,
练习6.2
(a) 返回类型不符
(b)无返回类型
(c)形参不能重名
(d)函数体必须用括号括起来
练习6.3 练习6.4(稍微一改就行)
#include <iostream>
using namespace std;
long long fact(int n)
{
if (n == 1)
return 1;
return n * fact(n - 1);
}
int main()
{
cout << fact(5) << endl;
return 0;
}
练习6.5
#include <iostream>
#include <cmath>
using namespace std;
int absolute(int a)
{
return abs(a);
}
int main()
{
cout << absolute(-1) << endl;
return 0;
}
练习6.6
形参和函数体内定义的变量都是局部变量.局部变量:函数开始时为形参申请存储空间,函数终止时,形参被销毁.
局部静态对象在程序的执行路径第一次经过对象定义语句时初始化,直到程序终止时才被销毁.换句话说,局部静态对象比普通局部变量声明周期长.
#include <iostream>
using namespace std;
int fun(int a)
{
int b = 2;
static int c = 1;
return a + b + c;
}
int main()
{
cout << fun(1) << endl;
return 0;
}
练习6.7
#include <iostream>
using namespace std;
int fun()
{
static int n = -1;
n++;
return n;
}
int main()
{
int i = 0;
for (i; i < 10; ++i)
cout << fun() << endl;
return 0;
}
练习6.8 6.9
略
练习6.10
#include <iostream>
using namespace std;
void Swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int a = 1, b = 2;
cout << "a =" << a << ", " << "b = " << b << endl;
Swap(&a, &b);
cout << "a =" << a << ", " << "b = " << b << endl;
return 0;
}
练习6.11
#include <iostream>
using namespace std;
void reset(int &a)
{
a = 0;
}
int main()
{
int a = 1;
cout << "a =" << a << endl;
reset(a);
cout <<"After reset: a = "<< a << endl;
return 0;
}
练习6.12
#include <iostream>
using namespace std;
void Swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 1, b = 2;
cout << "a =" << a << ", " << "b = " << b << endl;
Swap(a, b);
cout << "a =" << a << ", " << "b = " << b << endl;
return 0;
}
练习6.13
void f(T) 是按值传递 void f(&T) 是按引用传递
练习6.14
形参应该是引用类型: 传递参数太大,按值传递无效率. 或需要对原值修改
按值传递 : 不是上述情况
练习6.15
s是常量引用是因为函数不应对s做任何修改.occurs相反,必须修改.
c无需引用是因为就一个char类型,一个字节,按值传递不会损失什么效率.
s是普通引用的话,要特别注意不要对s修改,其他无差距.
occurs是常量引用则本函数错误.因为++occurs;这一句 尝试修改常量值.
练习6.16
就是8个字:尽量使用常量引用.
使用常量引用可以:a)避免对形参无意的修改 b)使得形参可以接受更多类型的实参
练习6.17
#include <iostream>
using namespace std;
bool judge(const string& s)
{
decltype(s.size()) i;
for ( i = 0; i < s.size(); ++i)
{
if (isupper(s[i]))
return true;
}
return false;
}
void change(string& s)
{
for (auto beg = s.begin(); beg != s.end(); ++beg)
if (isupper(*beg))
*beg = tolower(*beg);
}
int main()
{
string test;
while (cin >> test)
{
cout << test << endl;
if (judge(test))
change(test);
cout << test << endl;
}
return 0;
}
使用形参不同.判断是否有大小写字母的函数,使用常量引用.
练习6.18
(a) bool compare(const matrix& m1,const matrix& m2);
(b) vector<int> change_val(int ,vector<int> );
练习6.19
(a) 不合法.参数数量不匹配
练习6.20
尽量是常量引用,在需要对形参做更改时,再换成普通引用.
练习6.21
#include <iostream>
using namespace std;
int compare(int a, const int* p)
{
return a > * p ? a : *p;
}
int main()
{
int n = 2, a = 1;
int* p = &n;
cout << compare(a, p) << endl;
return 0;
}
练习6.22
#include <iostream>
using namespace std;
void Swap(int *p, int* q)
{
int temp = *p;
*p = *q;
*q = temp;
}
int main()
{
int a = 2, b = 1;
cout << "a = " << a << ",b = " << b << endl;
Swap(&a, &b);
cout << "a = " << a << ",b = " << b << endl;
return 0;
}
练习6.23
#include <iostream>
using namespace std;
void print(int i);
void print(int(&a)[2]);
void print(int(&a)[10]);
int main()
{
int i = 0, j[2] = { 0,1 }, k[10] = { 1,2,3,4,5,6,7,8,9,10 };
print(i);
print(j);
print(k);
return 0;
}
void print(int i)
{
cout << i << endl;
}
void print(int(&a)[2])
{
for (auto x : a)
cout << x << " ";
cout << endl;
}
void print(int(&a)[10])
{
for (auto x : a)
cout << x << " ";
cout << endl;
}
练习6.24
输出ia数组的元素.存在问题是:传入的ia数组长度并不一定是10个,函数体内可能越界.
修改:
void print(const int ia[10], size_t n)
{
for(size_t i = 0; i < n; ++i)
//同题目给出
}
练习6.25
#include <iostream>
using namespace std;
int main(int argc,char **argv)
{
string tmp;
for (int i = 1; i < argc; ++i)
{
tmp += argv[i];
tmp += " ";
}
cout << tmp << endl;
return 0;
}
练习6.26
不会!!!
练习6.27
#include <iostream>
#include <initializer_list>
#include <cstdarg>
using namespace std;
int Sum(initializer_list<int> lst)
{
int sum = 0;
for (const auto& x : lst)
sum += x;
return sum;
}
int main()
{
cout << Sum({ 1,2,3 }) << endl;
return 0;
}
练习6.28
string类型
练习6.29
可以声明,也可以不声明.
练习6.30
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;
}
报错: 函数必须返回值.