模版 取得一个数组中最大元素的下标

来源:互联网 发布:saas平台多租户 mysql 编辑:程序博客网 时间:2024/05/24 05:06
#include <iostream>using namespace std;template<typename T>int max(T a[], int n){int pos=0;for (int i=1; i<n; i++){if (a[pos] < a[i])pos = i;}return pos;}template<typename T>void show(T* a, int n){for (int i=0; i<n; i++)cout << a[i] << ' ';cout << endl;}int main(){int a[4] = {1, 4, 3, 7};double b[4] = {1.3, 4.5, 3.5, 7.8};char c[4] = {'a', 'g', 'c', 'b'};show(a,4);show(b,4);show(c,4);cout << max(a, 4) << endl;cout << max(b, 4) << endl;cout << max(c, 4) << endl;return 0;}

阅读全文
0 0
原创粉丝点击