c++数据结构与算法王立柱课后题第一章答案

文章介绍了C++中的几种基本概念和技术,包括如何实现swap函数交换变量,模板函数count和fill的应用,以及模板函数与普通函数的调用规则,还讨论了函数模板的局限性和解决方法,如重载和类型具体化。
摘要由CSDN通过智能技术生成

 

//1.如何实现一个swap函数交换x和y的值
#include <iostream>
using namespace std;

void swap(int& x, int& y);

void swap(int& x, int& y)
{
   
    int temp = x;
    x = y;
    y = temp;
}

int main()
{
    int a = 2;
    int b = 4;
    cout << "a= " << a << " b= " << b << endl;
    swap(a, b);
    cout << "a= " << a << " b= " << b << endl;

    return 0;
}
//2.编写一个模板函数count,返回值是数组a[0:n-1]的数值的个数。测试你的代码。
#include <iostream>
using namespace std;

template<class T>
int count(T a[], int n, const T& value)
{
	int theCount = 0;
	for (int i = 0; i < n; i++)
	{
		if (a[i] == value)	theCount++;
	}
	return theCount;
}

int main()
{
	int arr[10] = { 1,2,4,2,465,4,3457,34,56,4 };
	cout << "There have  " << count(arr, 10, 4) << " counts in this array!"<<  endl;

}
//3.编写一个模板函数fill,给数组a[start:end-1]赋值value
#include <iostream>
using namespace std;



template<class T>
void Fill(T a[],int n) 
{
	for (int i = 0; i < n; i++)
	{
		cout << "Please input a number for arr[" << i << "]: " << endl;
		cin >> a[i];
	}


	cout << "This is your array: ";

	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
}


int main()
{
	int arr[10] = { 0 };
	Fill(arr, 10);
}
//4.编写一个模板函数inner——product,返回值是∑a[i]*b[i]
#include <iostream>
using namespace std;



template<class T>
T inner_Product(T a[],T b[],int n)
{
	T sum = 0;
	for (int i = 0; i < n; i++)
		sum += a[i] * b[i];
	return sum;
}

int main()
{
	int arr[10] = { 1,234,43,56,345,46,567,43,55,67 };
	int sum = 0;
	cout << (sum = inner_Product(arr, arr, 10)) << endl;
}
5.普通函数和模版函数的调用规则
#include <iostream>
#include <cstring>

using namespace std;
//如果函数模板和普通函数都可以实现,优先调用普通函数
//可以通过空模板参数列表来强制调用函数模板
//函数模板也可以发生重载
//如果函数模板可以产生更好的匹配,优先调用函数模板


void myPrint(int a,int b) {
	cout << "putong" << endl;
}

template <class T>
void myPrint(T a, T b)
{
	cout << "调用模板" << endl;
}

template <class T>
void myPrint( T a, T b, T c)
{
	cout << "重载的模板" << endl;
}
void test01()
{
	int a = 10;
	int b = 20;
	myPrint(a, b);//此时如果只声明了普通函数,但是普通函数没有实现,那么此时会报错,仍然优先调用普通函数
	myPrint<>(a, b);//通过空模板参数列表,强制使用函数模板
	myPrint(a, b, 100);
	char c1 = 'a';
	char c2 = 'b';
	myPrint(c1, c2);//编译器认为如果用普通的,还要把char转换成int,倒不如把char直接转换成T,然后直接调用,就是只要推出T就行了

	//一般在实际开发中不会出现普通函数和模板函数同时出现的情况,但是如果真的出现了这种情况,要知道他的底层调用规则
}
int main()
{
	test01();


	system("pause");
	return 0;
}
//6.没有办法比较的时候如何处理?函数模板存在局限性
bool myCompare(T & a,T & b)
{
    if(a==b)
    else...
}

如果是传入Person?
1.函数重载  class MyClass {
public:
    int value;

    // 重载==运算符的成员函数版本
    bool operator==(const MyClass& other) const {
        return this->value == other.value;
    }
};

// 重载==运算符的友元函数版本
bool operator==(const MyClass& obj1, const MyClass& obj2) {
    return obj1.value == obj2.value;
}
2.//利用具体化Person的版本来实现代码,具体化优先调用
template <> bool myCompare(Person &a , Person & b)
{
if(p1.name==p2.name&&p1.age==p2.age)
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值