(1)用函数模板方法编写程序,对给各种不同类型的三个数分别求最大数。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
template <class T>
T findBiggest(T a, T b, T c)
{
return max({a,b,c});
}
int main()
{
int a = 5;
int b = 7;
int c = 4;
double x = 6.6;
double y = 7.99;
double z = 2.33;
char q = 'h';
char w = 'd';
char e = 'a';
cout << findBiggest(a, b, c) << endl;
cout << findBiggest(x, y, z) << endl;
cout << findBiggest(q, w, e) << endl;
cout << findBiggest<int>(a, x, q) << endl;
cout << findBiggest<double>(b, y, w) << endl;
cout << findBiggest<char>(c, z, e) << endl;
}
(2) 编程序创建一个类模板,可以对不同的数据类型的数组分别进行初始化、添加数据、求和、求平均值、显示数组等功能。
#include <iostream>
using namespace std;
template<class Arraytype>
class myArray
{
public:
myArray(int capacity)
{
myCapacity = capacity;
mySize = 0;
p = new Arraytype[myCapacity];
}
void Push_Back(const Arraytype& t)
{
if (myCapacity == mySize)
{
Arraytype* tmp = new Arraytype[myCapacity + 3];
for (int i = 0; i < mySize; i++)
{
tmp[i] = p[i];
}
delete p;
p = tmp;
myCapacity += 3;
}
p[mySize] = t;
mySize++;
}
myArray(const myArray& a)
{
this->myCapacity = a.myCapacity;
this->mySize = a.mySize;
this->p = new Arraytype[a.mySize];
for (int i = 0; i < a.mySize; i++)
{
this->p[i] = a.p[i];
}
}
myArray& operator=(const myArray& a)
{
if (&a == this)
{
return *this;
}
this->myCapacity = a.myCapacity;
this->mySize = a.mySize;
if (this->p != nullptr)
{
delete[]p;
p = nullptr;
}
this->p = new Arraytype[a.mySize];
for (int i = 0; i < a.mySize; i++)
{
this->p[i] = a.p[i];
}
return *this;
}
template<class T1>
T1 myAdd()
{
T1 temp = 0;
for (int i = 0; i < mySize; i++)
{
temp += p[i];
}
return temp;
}
template<class T2>
T2 myAdvrage()
{
T2 temp = 0.0;
int count = 0;
for (int i = 0; i < mySize; i++)
{
temp += p[i];
count++;
}
return temp / count;
}
void myPrint()
{
for (int i = 0; i < mySize; i++)
{
cout << p[i] << " ";
}
cout << endl;
}
~myArray()
{
myCapacity = 0;
mySize = 0;
if (p != nullptr)
{
delete[]p;
p = nullptr;
}
}
private:
Arraytype* p;
int myCapacity;
int mySize;
};
int main()
{
myArray<int> intArray(5);
intArray.Push_Back(10);
intArray.Push_Back(20);
intArray.Push_Back(30);
intArray.Push_Back(40);
intArray.Push_Back(50);
cout << intArray.myAdd<int>() << endl;
cout << intArray.myAdvrage<int>() << endl;
intArray.myPrint();
cout << endl;
myArray<double> doubleArray(5);
doubleArray.Push_Back(10.2);
doubleArray.Push_Back(20.6);
doubleArray.Push_Back(30.8);
doubleArray.Push_Back(40.4);
doubleArray.Push_Back(50.9);
cout << doubleArray.myAdd<double>() << endl;
cout << doubleArray.myAdvrage<double>() << endl;
doubleArray.myPrint();
cout << endl;
myArray<char> charArray(5);
charArray.Push_Back('g');
charArray.Push_Back('z');
charArray.Push_Back('h');
charArray.Push_Back('u');
charArray.Push_Back('A');
cout << charArray.myAdd<int>() << endl;
cout << charArray.myAdvrage<double>() << endl;
charArray.myPrint();
}
杂谈:
auto的使用可以简化代码量(暂时没发现有什么不妥)
//迭代器
for(auto it=v.begin();it!=v.end();it++)
{
}
//本题函数
auto myAdd()
{
auto temp = 0;
for (int i = 0; i < mySize; i++)
{
temp += p[i];
}
return temp;
}
auto myAdvrage()
{
auto temp = 0.0;
int count = 0;
for (int i = 0; i < mySize; i++)
{
temp += p[i];
count++;
}
return temp / count;
}
只做了基本数据类型的模板化,类类型等没有实现
文章展示了如何使用C++的函数模板来找到不同类型的三个数中的最大值,以及如何创建一个类模板实现动态数组的功能,包括初始化、添加数据、求和、求平均值和显示数组。同时,文章提到了使用auto关键字简化代码的示例。
1万+

被折叠的 条评论
为什么被折叠?



