1.
#includeusing namespace std; int n = 0; void show(const char* a,int b=0) { n++; if (b == 0) { cout << a< 2.
#includeusing namespace std; struct candybar { const char *name;//必须加const ,原因还未知。 double heavy; int hot; }; void cand(candybar& a, const char* b = "Millennium Munch",const double c = 2.85,const int d = 350) { a.name = b; a.heavy = c; a.hot = d; } void show(candybar& a) { cout << "name:" << a.name< 3.
#include#include using namespace std; void transfer(string& a) { for (int i = 0; i < a.size(); i++) { if (a[i] == ' ') continue; a[i] = a[i] - 32; } } int main() { string a; cout << "Enter a string (q to quit):"; getline(cin,a); while (a[0] != 'q' || a.size() > 1) { transfer(a); cout << a< 4.
#includeusing namespace std; #include struct stringy { char* str; int ct; }; void set(stringy& a, char* ch) { a.str = new char[strlen(ch) + 1]; a.ct = strlen(ch); a.str = ch; } void show(const stringy&a,const int b = 1) { for(int i=0;i 5.
#includeusing namespace std; template T show(T *a) { T max = 0; for (int i = 0; i < 5; i++) { if (i == 0) max = a[i]; else if (a[i] > max) max = a[i]; } return max; } int main() { int a[5] = { 2,1,56,3565,489 }; double b[5] = { 135.23,13.12,487.66,45.12,56544.12 }; cout << show(a)< 6.
#includeusing namespace std; template T maxn(T A[], int len) { T max=A[0]; for (int i = 0; i < len; i++) max = A[i] > max ? A[i] : max; return max; } template<>char* maxn(char* a[], int len) { int max = -1; char* p = NULL; for (int i = 0; i < len; ++i) { if (strlen(a[i]) > max) { max = strlen(a[i]); p = a[i]; } } return p; } int main() { int a[6] = { 1, 2, 3, 4, 5, 6 }; double b[4] = { 1.1, 2.2, 3.3, 4.4 }; const char* c[5] = { "fuck", "fuckfuck", "fuckfuckfuck", "fuckfuckfuckfuck", "fuckfuckfuckfuckfuck" }; cout << maxn(a, 6) << endl; cout << maxn(b, 4) << endl; cout << maxn(c, 5) << endl; return 0; } 7.
#includetemplate // template A void SumArray(T arr[], int n); template // template B void SumArray(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]; // set pointers to the amount members of the structures in mr_E for (int i = 0; i < 3; i++) pd[i] = &mr_E[i].amount; cout << "Listing Mr. E's counts of things:n"; // things is an array of int SumArray(things, 6); // uses template A cout << "Listing Mr. E's debts:n"; // pd is an array of pointers to double SumArray(pd, 3); // uses template B (more specialized) cin.get(); return 0; } template void SumArray(T arr[], int n) { using namespace std; T sum = 0; cout << "template An"; for (int i = 0; i < n; i++) sum = sum + arr[i]; cout << sum << endl; } template void SumArray(T* arr[], int n) { using namespace std; T sum = 0; cout << "template Bn"; for (int i = 0; i < n; i++) sum = sum + *arr[i]; cout << sum << endl; } 第七题直接抄的,感觉知识点都一样



