上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 49 下一页
摘要: //第二十三模板 11模板参数提供默认值/*#include <iostream>#include <string>using namespace std;//默认参数 class T2=inttemplate<class T1, class T2=int>class People{public: T1 &getx(); T2 &gety(); T1 getx()const{ return x;} T2 gety()const{ return y;} People(const T1&a, const T2&b):x(a),y( 阅读全文
posted @ 2012-10-04 19:57 简单--生活 阅读(473) 评论(0) 推荐(0) 编辑
摘要: //第二十三模板 12约束模板//结束模板的大意,即是对模板的数据和方法有要求/*template<class T>T max(T a, T b){ return (a>b)?a:b;}假如比较的类型是两个指向字符串的指针,则比较运算符>比较的是两个地址而不是数值char* max(char *a, char *b){ if(strcmp(a,b) > 0) return a; else return b;}该函数具体化了max函数的两个参数和功能,该函数的两个参数是char*,它的功能是使用strcmp函数比较指针所指向的字符串而不是指针,这个特定的用来替换函数 阅读全文
posted @ 2012-10-04 19:57 简单--生活 阅读(273) 评论(0) 推荐(0) 编辑
摘要: //第二十三模板 13模板成员//模板可作为类,结构甚至模板类的成员/*#include <iostream>using namespace std;template<class T1>class One{public: //不知道这里的i是什么意思 //One(T1 t, int i):first(t),second(t){} One(T1 t, int i):first(t),second(i){} template<class T2> T2 get(T2 t2, T1 t1){ cout<<"second.get()返回" 阅读全文
posted @ 2012-10-04 19:57 简单--生活 阅读(141) 评论(0) 推荐(0) 编辑
摘要: //第二十三模板 14将模板用作参数//模板除了可以包括类型参数(class T)和非类型参数(int n)之外,还要台将模板作为参数包括进去//tempalte<calss T>//template<template<class T>class T1>//这个T1类型有局限性,它要求我们在使用该模板时传递的参数必须是个模板类//people<human>Jack;//其中的human是用a模板声明的模板类,而people则是用b模板声明的模板类, Jack是用people这个模板类定义的一个对像/*#include <iostream&g 阅读全文
posted @ 2012-10-04 19:57 简单--生活 阅读(369) 评论(0) 推荐(0) 编辑
摘要: //第二十三模板 8数组模板/*#include <iostream>using namespace std;template<class T, int n>class people{public: people(); people(const T&t); T&operator[](int i); //注意: //这两行定义的带一个参数的构造数和下标运算符数operator[],它们操作的对像不是下面定义的数组成员a[] //而是people类的对像 void show();private: T a[n];};template<class T, i 阅读全文
posted @ 2012-10-04 19:56 简单--生活 阅读(227) 评论(0) 推荐(0) 编辑
摘要: //第二十三模板 9对像数组模板//我们也可以直接使用对像模板来构造对像数组//people<people<int,5>,10>two;//该语句创建了一个包含10个元素对像数组two,每个对像又拥有一个包含5个int元素的数组,因此它可以看作如下二维数组//int two[10][5]//注意,在模板的表示语法中,维的顺序与正常的二维数组的顺序相反//people<people<int,5>,10>one; 可看做是 one[10][5];/*#include <iostream>using namespace std;templa 阅读全文
posted @ 2012-10-04 19:56 简单--生活 阅读(198) 评论(0) 推荐(0) 编辑
摘要: //第二十三模板 10具有多个参数的模板/*#include <iostream>#include <string>using namespace std;template<class T1, class T2>class People{public: T1 &getx(); T2 &gety(); T1 getx()const{ return x;} T2 gety()const{ return y;} People(const T1&a, const T2&b):x(a),y(b){ cout<<"构 阅读全文
posted @ 2012-10-04 19:56 简单--生活 阅读(228) 评论(0) 推荐(1) 编辑
摘要: //第二十三模板 4普通函数,函数模板与具体化函数模板的优先级//我们定义了个普通函数,然后又定义了一个该函数的模板,接着又重载了这个函数的模板,那么这里就存在一个优先级的问题,即先调用哪里个函数//1 普通函数和函数模板的执行次序/*#include <iostream>using namespace std;template <class T>void show(T a){cout<<"模板函数"<<endl;}void show(int a){cout<<"普通函数"<<end 阅读全文
posted @ 2012-10-04 19:55 简单--生活 阅读(379) 评论(0) 推荐(1) 编辑
摘要: //第二十三模板 5函数模板的匹配/*#include <iostream>using namespace std;struct people{ char name[10]; int age;};template <class T>void show(T t[], int n){ cout<<"执行函数模板void show(T t[], int n) "<<endl; for(int i=0; i<n; i++) cout<<t[i]<<' '; cout<<endl 阅读全文
posted @ 2012-10-04 19:55 简单--生活 阅读(107) 评论(0) 推荐(0) 编辑
摘要: //第二十三模板 6类模板的定义//类模板的定义与函数模板的定义大到到致相同/*#include <iostream>using namespace std;template<class T>class people{public: people(T x, T y):X(x),Y(y){} T getX(){ return X;} T getY(){ return Y;}private: T X, Y;};int main(){ int a=3, b=4; people<int> Jack(a,b); //注意,由于people是个模板类,因此构造对像时略有 阅读全文
posted @ 2012-10-04 19:55 简单--生活 阅读(146) 评论(0) 推荐(0) 编辑
上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 49 下一页
简单--生活(CSDN)