数据结构、算法与应用 C++语言描述(第二版)第一章1-10

本文深入探讨了C++模板函数的应用,包括参数类型传递、数组操作、内积计算、异常处理等关键概念。通过具体实例,如数组元素计数、数组填充、内积计算、数组初始化、数组排序检查及数组比较,详细解析了模板函数如何提高代码复用性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1题:知识点:函数参数类型分三种:
1、传值参数:传入的是参数的拷贝,对拷贝的操作并不能改变参数本身。
2、引用参数:传入的是参数的引用,也即参数的类型别名,可以被函数修改。
3、常量引用参数:传入的是参数的引用,但不能被函数修改。
本题:x, y 为传值类型,在函数里面的值为 x, y 值的拷贝,因此交换函数 swap 改变不了x, y 的值。若想达到目的,只需将传值参数类型改为引用参数类型即可:

void swap(int &x, int &y)

2题:传入参数为3个,分别是数组指针,元素个数和检测值

#include<iostream>
using namespace std;
template<class T>    
int count(T *arr, int n, const T &value)
{
	int num = 0;
	for (int i = 0; i < n; ++i)
	{		
		if (arr[i] == value)
			++num;
	}
	return num;
}
void main()
{
	int arr[10] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
	const int value = 0;
	cout << count(arr, 10, value) << endl;
	system("pause");
}

3题:同2,不过传入的不是元素数量,而是数组操作区间。

#include<iostream>
using namespace std;
template <class T>
void fill(T* a, int start, int end, const T& value)
{
	for (int i = start; i < end; i++)
		a[i] = value;
}
void main()
{
	int arr[10] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
	const int value = 0;
	fill(arr, 5, 10, value);   //从下标5开始填充值至数组结束。 
	for (auto a : arr)
		cout << a << ends;
	cout << endl;
	system("pause");
}

4题:

#include<iostream>
using namespace std;
template <class T>
T inner_product(T* a, T* b, const int &n)    //计算内积
{
	T total = 0;
	for (int i = 0; i < n; i++)    
		total += a[i] * b[i];
	return total;
}
void main()
{
	const int n = 10;
	int arr1[n] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
	int arr2[n] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
	cout << inner_product(arr1, arr2, n) << endl;   
	system("pause");
}

5题:

#include<iostream>
using namespace std;
template <class T>
void iota(T* a, int n, const T& value)
{// Set a[i] = value + i, 0 <= i < n.
	for (int i = 0; i < n; i++)
		a[i] = value + i;
}
void main()
{
	const int value = 1;
	int arr[10] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
	iota(arr, 10, value);
	for (auto a : arr)
		cout << a << ends;
	cout << endl;
	system("pause");
}

6题:

#include<iostream>
#include<string>
using namespace std;
template <class T>
bool is_sort(T* a, const int n, const string &compare)
{
	int num = 0;
	if (compare == "up")
		for (int i = 0; i < n - 1; ++i)
			if (a[i] < a[i + 1])
				++num;
	else
		for (int i = 0; i < n - 1; ++i)
			if (a[i] > a[i + 1])
				++num;
	return (num == n - 1);
}
void main()
{
	const int n = 10;
	const string compare = "up";
	int arr[n] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	if(is_sort(arr, n, compare))
		cout<<"This is a "<< compare << " array!"<< endl;
	system("pause");
}

7题:有序数组,就是指其数组元素按升序或降序排列的数组。

#include<iostream>
using namespace std;
template <class T>
int mismatch(T* a, T* b, const int n)
{
	for (int i = 0; i < n; i++)
		if (a[i] != b[i])
			return i;
	return n;
}
void main()
{
	const int n = 10;
	int arr1[n] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int arr2[n] = { 0, 1, 2, 3, 4, 5, 4, 3, 2, 1 };
	if (mismatch(arr1, arr2, n) == n)    //全部匹配时
		cout << "两数组一样!" << endl;
	else
		cout << "第一对不相等元素的索引: " << mismatch(arr1, arr2, n) << endl;
	system("pause");
}

8题:知识点:一个函数的签名,是由其形参类型以及形参个数确定的,且任意两个同名函数不能有同样的签名。
本题中:两个函数签名一样。

9题:1-1 中签名为(int, int, int),1-2 中签名为(float, float, float)。本题中:
1)调用第1个
2)调用第2个
3)报错:多个实例与参数列表匹配。因为从 float 转为 int 和从 int 转为 float 都可以,造成二义性。
4)调用第2个

10题:注意抛出异常的类型。

#include<iostream>
using namespace std;
int abc(int a, int b, int c)
{
	if (a < 0 & b < 0 & c < 0)     //abc全为负,则抛出1
		throw 1;
	if (a == 0 & b == 0 & c == 0)    //abc全为0,则抛出2
		throw 2;
	return a + b*c;
}
int main()
{
	int a = 0, b = 0, c = 0;
	try{ cout << abc(a, b, c) << endl; }
	//try的处理块
	catch (int e)    //异常类型为int
	{
		cout << "The parameters to abc were " << a << ends << b << ends << c << endl;
		cout << "An exception has been thrown\n";
		cout << e << endl;
		system("pause");
		return 1;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值