#include <iostream> #include <string> using namespace std; template <typename T,int n> void Max(T a[]) { T max=a[0]; for (int i = 1; i < n; i++) max = max < a[i] ? a[i] : max; cout << "最大值是:" << max << endl; } void Max(char * a, int n) { char max =a[0]; for (int i = 1; i < n; i++) max = max < a[i] ? a[i] : max; cout << "最大值是:" << max << endl; } int main() { string s[] = { "s123","s12","s12345","s12365" }; int a[] = { 3,33,44,6,76,43,66 }; char* b = (char*)"ahgdssev"; Max<int, 7>(a); Max<string, 4>(s); Max(b, sizeof(b)); //Max<char*, sizeof(b)>(b); return 0; }有没有实现普通函数重载,是怎么实现的
时间: 2024-04-04 19:34:46 浏览: 10
在这段代码中,函数重载 `Max` 就是普通函数重载。普通函数重载是指在同一作用域内,定义了多个同名但参数类型或数量不同的函数,编译器会根据调用时实参的类型和数量来自动选择对应的函数进行调用。
在这段代码中,函数 `Max` 有两个重载版本,一个是模板函数,一个是针对字符数组的非模板函数。这两个函数的参数类型和数量不同,因此可以实现重载,当调用 `Max` 函数时,编译器会自动选择合适的函数进行调用。
相关问题
#include <iostream> #include <string> using namespace std; template <typename T,int n> void Max(T a[]) { T max=a[0]; for (int i = 1; i < n; i++) max = max < a[i] ? a[i] : max; cout << "最大值是:" << max << endl; } void Max(char * a, int n) { char max =a[0]; for (int i = 1; i < n; i++) max = max < a[i] ? a[i] : max; cout << "最大值是:" << max << endl; } int main() { string s[] = { "s123","s12","s12345","s12365" }; int a[] = { 3,33,44,6,76,43,66 }; char* b = (char*)"ahgdssev"; Max<int, 7>(a); Max<string, 4>(s); Max(b, sizeof(b)); //Max<char*, sizeof(b)>(b); return 0; }
这段代码是一个使用函数模板和函数重载的例子。其中,函数模板 `Max` 可以接受任意类型的数组和数组长度,通过遍历数组找到其中最大的元素并输出。而函数重载 `Max` 则专门用于处理字符数组,因为字符数组的比较需要使用字符的 ASCII 码值。在主函数中,分别使用了 `Max` 模板函数和 `Max` 函数重载来比较不同类型的数组。
创建函数模板1.创建Student(数据成员string name ,int grade)类及所需函数。2.求出集合中的最大值,支持int,double,char,Student等类型的数据。3.排序方法,支持int,double, char,Student (按成绩排序) 等类型的数据。4.中位数指的是一组数,如果按照大小排序排好后最中间的那个数的值, 如果有偶数个元素,那么就是最中间两个数的平均数。求出集合中的中位数,支持int,double,Student集合中成绩的中位数。
非常抱歉,刚才的回答中代码存在一些错误,现在我给你重新演示一下符合要求的代码:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<typename T>
class Student {
public:
string name;
T grade;
Student(string n, T g) : name(n), grade(g) {}
bool operator<(const Student<T>& other) const {
return grade < other.grade;
}
};
template<typename T>
T findMax(const vector<T>& v) {
T maxVal = v[0];
for (const T& val : v) {
if (val > maxVal) {
maxVal = val;
}
}
return maxVal;
}
template<typename T>
void sortVector(vector<T>& v) {
sort(v.begin(), v.end());
}
template<typename T>
T findMedian(vector<T>& v) {
int n = v.size();
sortVector(v);
if (n % 2 == 0) {
return (v[n / 2 - 1] + v[n / 2]) / 2;
}
else {
return v[n / 2];
}
}
int main() {
vector<int> v1 = { 3, 5, 1, 7, 2, 8 };
cout << "Max value in v1: " << findMax(v1) << endl;
sortVector(v1);
cout << "Sorted v1: ";
for (const int& val : v1) {
cout << val << " ";
}
cout << endl;
cout << "Median value in v1: " << findMedian(v1) << endl;
vector<double> v2 = { 3.5, 5.2, 1.8, 7.1, 2.6, 8.9 };
cout << "Max value in v2: " << findMax(v2) << endl;
sortVector(v2);
cout << "Sorted v2: ";
for (const double& val : v2) {
cout << val << " ";
}
cout << endl;
cout << "Median value in v2: " << findMedian(v2) << endl;
vector<char> v3 = { 'c', 'a', 'b', 'e', 'd', 'f' };
cout << "Max value in v3: " << findMax(v3) << endl;
sortVector(v3);
cout << "Sorted v3: ";
for (const char& val : v3) {
cout << val << " ";
}
cout << endl;
cout << "Median value in v3: " << findMedian(v3) << endl;
vector<Student<int>> v4 = { Student<int>("Alice", 85), Student<int>("Bob", 75), Student<int>("Charlie", 90) };
cout << "Max value in v4: " << findMax(v4).grade << endl;
sortVector(v4);
cout << "Sorted v4: ";
for (const Student<int>& val : v4) {
cout << val.grade << " ";
}
cout << endl;
cout << "Median value in v4: " << findMedian(v4).grade << endl;
vector<Student<double>> v5 = { Student<double>("Alice", 85.5), Student<double>("Bob", 75.2), Student<double>("Charlie", 90.1) };
cout << "Max value in v5: " << findMax(v5).grade << endl;
sortVector(v5);
cout << "Sorted v5: ";
for (const Student<double>& val : v5) {
cout << val.grade << " ";
}
cout << endl;
cout << "Median value in v5: " << findMedian(v5).grade << endl;
return 0;
}
```
这是一个使用了函数模板的程序,包含了创建Student类及所需函数、求出集合中的最大值、排序方法以及中位数的计算。程序中的Student类是一个模板类,支持不同类型的成绩。findMax函数和sortVector函数都是模板函数,能够处理不同类型的数据。findMedian函数也是一个模板函数,能够处理包括int、double和Student类中成绩的中位数的计算。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)