第六章 6.2 参数传递

来源:互联网 发布:ipadpro下载软件流程 编辑:程序博客网 时间:2024/06/11 16:53

6.10编写一个函数,使用指针形参交换两个整数的值。在代码中调用该函数并输出交换后的结果。

#include<iostream>using namespace std;void changeAB(int *a, int *b);int main(){    int a = 5,b = 6;    cout << "before:" << a << " " << b << endl;    changeAB(&a,&b);    cout << "after:" << a << " " << b <<endl;}void changeAB(int *a,int *b){    int temp = *a;    *a = *b;    *b = temp;}

6.17编写一个函数,判断string对象中是否含有大写字母。编写另一个函数,把string对象全部改写成小写形式。

#include<iostream>#include<string>using namespace std;bool is_super(const string s);void change_low(string *s);//void change_low(string &s);int main(){    cout << "请输入字符串:";    string s = "";    cin >> s;    cout << is_super(s) <<endl;    change_low(&s);    cout << s <<endl;    return 0;}bool is_super(const string s){    for(auto c : s)    {        if(isupper(c))            return true;    }    return false;}void change_low(string *s){    for(auto &c : *s)    {        if(isupper(c))        {            c = tolower(c);        }    }}/*void change_low(string &s){    for(auto &c : s)    {        if(isupper(c))        {            c = tolower(c);        }    }}*/

6.18为下面的函数编写函数声明。
(1)bool compare(const &matrix a, const &matrix b);//比较两个matrix类是否相等
(2)vector<int> change_val(int a, *vercto<int> iv);//


6.21编写一个函数,令其接受两个参数:一个是int型的数,一个是int指针。函数比较int的值和指针所指的值,返回较大的那个。

#include<iostream>using namespace std;int print(const int, const int*);int main(){    int a = 7, b[] = {1,2};    int c = print(a,b);    cout << c << endl;}int print(const int a, const int* b){    return a > *b ? a : *b;}

6.22编写一个函数,令其交换两个int指针。

#include<iostream>using namespace std;void swap(int*&, int*&);int main(){    int a = 7, b = 8;    int *p = &a,*q = &b;    cout << *p << " " << *q << endl;    swap(p,q);    cout << *p << " " << *q << endl;    cout << a << " " << b << endl;}void swap(int*& a, int*& b){    int* temp = a;    a = b;    b = temp;}

6.25编写一个main函数,令其接受两个实参,把实参内容连接成一个string对象并输出出来。

#include<iostream>#include<string>using namespace std;int main(int argc, char *argv[]){    string a = argv[1];    cout << a << endl;    string b = argv[2];    cout << b << endl;    cout << a + b << endl;}
0 0
原创粉丝点击