cpp 8.14

来源:互联网 发布:手机卡没办法收到网络 编辑:程序博客网 时间:2024/06/16 01:19

8.14

#include<iostream>template <typename T>void ShowArray(T arr[], int n);template<typename T>void ShowArray(T *arr[], int n);struct debts{char name[50];double amount;};int main(){using namespace std;int things[6] = { 13,31,103,301,310,130 };struct debts mr_E[3] ={{"Ima Wolfe", 2400.0},{"Ura Foxe", 1300.0},{"Iby stout", 1800.0}};double * pd[3];for (int i = 0; i < 3; i++)pd[i] = &mr_E[i].amount;cout << "Listing Mr. E's counts of things:\n";ShowArray(things, 6);cout << "Listing Mr E's debts:\n";ShowArray(pd, 3);system("pause");return 0;}template<typename T>void ShowArray(T arr[], int n){using namespace std;cout << "template A\n";for (int i = 0; i < n; i++)cout << arr[i] << ' ';cout << endl;}template <typename T>void ShowArray(T * arr[], int n){using namespace std;cout << "template B\n";for (int i = 0; i < n; i++)cout << *arr[i] << ' ';cout << endl;}


8.15

#include<iostream>\template<class T>T lesser(T a, T b){return a < b ? a : b;}int lesser(int a, int b){a = a < 0 ? -a : a;b = b < 0 ? -b : b;return a < b ? a : b;}int main(){using namespace std;int m = 20;int n = -30;double x = 15.5;double y = 25.9;cout << lesser(m, n) << endl;cout << lesser(x, y) << endl;cout << lesser<>(m, n) << endl;cout << lesser<int>(x, y)<<endl;system("pause");return 0;}


0 0
原创粉丝点击