C++ Primer 第五版 第6章 6.2——函数参数传递习题答案

版权声明:本文为博主原创文章,未经允许,不得转载!欢迎留言,允许附带链接转载! https://blog.csdn.net/Sophia_11/article/details/88261428

理论讲解请参考:C++ Primer 第五版 第6章 6.2——函数参数传递阅读笔记


目录

6.10 指针形参交换两个数

6.12 引用形参交换两个数

6.13 

6.14

6.15 

6.16 

6.17

6.18 

6.19

6.20 

6.21 

6.22

6.24

6.25

6.27


6.10 指针形参交换两个数

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

void fun(int *v1,int *v2)
{
    int i = 0;
    int * temp = &i;
    *temp = *v1;
    *v1 = *v2;
    *v2 = *temp;
}

int main()
{
    int m = 6;
    int n = 9;
    cout << "m: " << m << "  n: " << n << endl;
    fun(&m, &n);   //这里调用,参数是地址,因为形参是指针,区别于形参是引用
    cout << "交换后的值为:" << endl;
    cout << "m: " << m << "  n: " << n << endl;
    return 0;
}

运行结果:


6.12 引用形参交换两个数

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

void fun(int &v1,int &v2)
{
    int i = 0;
    int &temp = i;
    temp = v1;
    v1 = v2;
    v2 = temp;
}

int main()
{
    int m = 6;
    int n = 9;
    cout << "m: " << m << "  n: " << n << endl;
    fun(m, n);   //这里参数只需对象就行,不需要地址
    cout << "交换后的值为:" << endl;
    cout << "m: " << m << "  n: " << n << endl;
    return 0;
}

运行结果:


6.13 

(1)f(T)是值传递,不能改变实参的值

(2)f(&T)是引用传递,相当于实参的另一个名字,可以改变实参的值


6.14

如果在程序中需要被修改,则需要声明为引用类型


6.15 

s:在执行函数期间,s值不会被改变

occurs:occurs值需要被改变

c:是一个临时变量,所以不需要使用引用类型

如果令occurs为常量引用,则输出occurs为0(不能加以修改),

如果s生命为普通引用可能会在程序中被修改


6.16 

细节:尽量使用常量引用

应定义s为常量引用:const string& s


6.17

第一个只需判断是否含有大写字母,没有涉及到改变值,所以形参定义为常量引用

第二个需要更改值,所以形参为普通引用

判断是否有大写字母(形参为常量引用):

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

// 形参常量引用
void fun(const string& str)
{
    for (int i = 0; i != str.size(); ++i)
    {
        if (str[i] >= 'A' && str[i] <= 'Z')   //判断是否存在大写字母
            cout << "存在大写字母!" << endl;
        else
            cout << "不存在大学字母!" << endl;
    }
}

int main()
{
    cout << "Please enter strings:";
    string tr;
    cin >> tr;
    fun(tr);
    return 0;
}

将大写字母改为小写(形参为普通引用):

知识点:将大写字母改为小写,则是加32。如果将小写改为大写,则是减32。

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

void fun(string& str)
{
    for (int i = 0; i != str.size(); ++i)
    {
        if (str[i] >= 'A' && str[i] <= 'Z')  // 判断是否大写
            str[i] += 32;  //加32改为小写
    }
}

int main()
{
    cout << "Please enter strings:";
    string tr;
    cin >> tr;
    fun(tr);
    for (int i = 0; i != tr.size(); ++i)
        cout << tr[i];
    cout << endl;
    return 0;
}

引申一下,将字符串小写改为大写大写改为小写:

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

void fun(string& str)
{
    for (int i = 0; i != str.size(); ++i)
    {
        if (str[i] >= 'A' && str[i] <= 'Z')  // 判断是否大写
            str[i] += 32;  //加32改为小写
        else
            str[i] -= 32;  //键32改为大写
    }
}

int main()
{
    cout << "Please enter strings:";
    string tr;
    cin >> tr;
    fun(tr);
    for (int i = 0; i != tr.size(); ++i)
        cout << tr[i];
    cout << endl;
    return 0;
}

6.18 

(a)

//功能可能是比较两个matrix的大小
bool compare(const matrix &m1, const matrix &m2) {  /.../   }

(b)

vector<int>::iterator change_val(int, vector<int>::iterator) { /.../ }

6.19

(a):不合法,函数只有一个参数

(b):合法

(c):合法

(d):合法


6.20 

函数不改变形参值时应该是常量引用。

如果将应该是常量引用设置为普通引用,则可能会修改其指造成错误


6.21 

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

int fun(int v1, const int *cp)
{
    int max=v1;
    if (v1 < *cp)
        max = *cp;
    return max;
}

int main()
{
    
    int n1, n2;
    cout << "Please enter two numbers: " << endl;
    cin >> n1 >> n2;
    int *p = &n2;
    int Max=fun(n1,p);
    cout <<"较大值为: " << Max<<endl;
    return 0;
}

6.22

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

int fun( int *cp1,  int *cp2)
{
    int m = 0;
    int *Temp=&m;
    *Temp = *cp1;
    *cp1 = *cp2;
    *cp2 = *Temp;
    return 0;
}

int main()
{
    
    int n1, n2;
    cout << "Please enter two numbers: " << endl;
    cin >> n1 >> n2;
    int *p1 = &n1;
    int *p2 = &n2;
    cout << "交换前的值为:" << *p1 << ' ' << *p2 << endl;
    fun(p1,p2);
    cout <<"交换后的值为:" << *p1<<' '<< *p2<<endl;
    return 0;
}

6.24

此题以值传递数组,是错误的。可以用下边方法传递,数组的引用:

void print(const int (&ia)[10])   {/*...*/}

6.25

#include <iostream>
#include <string>
 
int main(int argc, char** argv)//实参列表
{
    string str;
    for (int i = 1; i != argc; ++i) {
        str += argv[i];   //因为argv是从1开始,不是从0开始
        str += " ";
    }
 
    cout << str <<endl;
    return 0;
}

6.27

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

int fun(initializer_list<int> lst)
{
    int sum = 0;
    for (auto beg = lst.begin(); beg!= lst.end(); ++beg)
        sum+=*beg;
    return sum;
}
void main()
{
    int Sum = fun({ 3, 4, 5, 6, 7, 8, 9 });
    cout << Sum << endl;
}

猜你喜欢

转载自blog.csdn.net/Sophia_11/article/details/88261428