C++primer5 6.10/6.17/6.21/6.22/6.23

6.10 编写一个函数,使用指针(引用)形参交换两个整数的值

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

/*void swap(int *num1, int *num2)   //形参使用指针
{
	int tmp;
	tmp = *num1;
	*num1 = *num2;
	*num2 = tmp;
}*/

void swap(int &num1, int &num2) //形参使用引用
{
	int tmp;
	tmp = num1;
	num1 = num2;
	num2 = tmp;
}

int main()
{
    cout << "please input two number: " << endl;
	int num1,num2;
	cin>>num1>>num2;
	cout << "before switch: " << num1 << " " << num2 << endl;
	//swap(&num1, &num2); //函数调用指针型形参
	swap(num1, num2);     //函数调用引用型形参
	cout << "after switch: " << num1 << " " << num2 << endl;

	system("pause");
    return 0;
}

6.17 编写一个函数,判断string对象中是否有大写字母。编写另一个函数,把string对象全部改成小写形式。注意两个函数的形参的区别。

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

bool any_capital(string const &str)
{
    for (auto ch : str)  //遍历是否有大写,不修改实参使用const引用
    {
        if (isupper(ch)) 
            return true;
    }    
    return false;
}

void to_lowercase(string &str)
{
    for (auto &ch : str) //改变大写为小写,使用引用修改实参
         ch = tolower(ch);
}

int main()
{
    string hello("Hello World!");
    cout << any_capital(hello) << endl;

    to_lowercase(hello);
    cout << hello << endl;

    system("pause");
    return 0;
}

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

//6.21
#include <iostream>
#include <string>
using namespace std;

int larger_one(const int i, const int *const p)
{
    return (i > *p) ? i : *p; //比较并返回较大者
}

int main()
{
    int i = 6;
    cout << larger_one(7, &i) << endl;

    system("pause");
    return 0;
}

6.22 编写函数,交换两个int指针

//6.22
#include <iostream>
#include <string>
using namespace std;

void swap(int* &num1, int* &num2) //交换函数,交换两个int指针
{
    auto tmp = num1;
    num1 = num2;
    num2 = tmp;
}

int main()
{
    int i = 42, j = 99;
    auto num1 = &i;
    auto num2 = &j;                      //打印int指针
    std::cout << "before switching: " << *num1 << " " << *num2 << std::endl;
    swap(num1, num2);                    //打印int指针
    std::cout << "after switching: " << *num1 << " " << *num2 << std::endl;
    std::cout << "after switching i j: " << i << " " << j << std::endl;

    system("pause");
    return 0;
}

## 6.23 使用几个print函数依次打印数组

```cpp
//6.23 数组形参
#include <iostream>
#include <string>
using namespace std;

void print(const int *pi)
{
    if(pi)
        cout << *pi << endl;
}

void print(const char *p) //使用标记指定数组长度
{
    if (p)
        while (*p) cout << *p++;
    cout << endl;
}

void print(const int *beg, const int *end) //使用标准库规范
{
    while (beg != end)
        cout << *beg++ << endl;
}

void print(const int ia[], size_t size) //显示传递一个表示数组大小的形参
{
    for (size_t i = 0; i != size; ++i) {
        cout << ia[i] << endl;
    }
}

void print(int (&arr)[2]) //数组引用形参
{
    for (auto i : arr)
        cout << i << endl;
}

int main()
{
    int i = 0, j[2] = { 0, 1 }; //大小为2的整型数组
    char ch[5] = "pezy";
    //调用函数重载
    print(ch);
    print(begin(j), end(j));
    print(&i);
    print(j, end(j)-begin(j)); //通过第二个实参确定要输出多少个元素-数组的大小
    print(j);                  //实参是含有2个整数的数组

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值