code:
/*第16章 模板与泛型编程 52516.1 模板定义16.2 实例化16.3 模板编译模型16.4 类模板成员16.5 一个泛型句柄类16.6 模板特化16.7 重载与函数模板小结第16章 模板与泛型编程 52516.1 模板定义 52616.1.1 定义函数模板 52616.1.2 定义类模板 52816.1.3 模板形参 52916.1.4 模板类型形参 53116.1.5 非类型模板形参 53316.1.6 编写泛型程序 53416.2 实例化 53516.2.1 模板实参推断 53716.2.2 函数模板的显式实参 54016.3 模板编译模型 54216.4 类模板成员 54516.4.1 类模板成员函数 54816.4.2 非类型形参的模板实参 55116.4.3 类模板中的友元声明 55216.4.4 Queue和QueueItem的友元声明 55416.4.5 成员模板 55616.4.6 完整的Queue类 55816.4.7 类模板的static成员 55916.5 一个泛型句柄类 56016.5.1 定义句柄类 56116.5.2 使用句柄 56216.6 模板特化 56416.6.1 函数模板的特化 56516.6.2 类模板的特化 56716.6.3 特化成员而不特化类 56916.6.4 类模板的部分特化 57016.7 重载与函数模板 570小结 573术语 574*///第16章 模板与泛型编程//16.1 模板定义 -------------------------------------------------------------------------------------------------------- #include#include using namespace std;// returns 0 if the values are equal, -1 if v1 is smaller, 1 if v2 is smallerint compare(const string &v1, const string &v2){ if(v1 < v2) return - 1; if(v2 < v1) return 1; return 0;}int compare(const double &v1, const double &v2){ if(v1 < v2) return - 1; if(v2 < v1) return 1; return 0;}int main(){ cout << compare("abc","def") << endl; cout << compare(3.14,3.1415) << endl; return 0;}// 函数模板 function template#include #include using namespace std;// implement strcmp-like generic compare function// returns 0 if the values are equal, 1 if v1 is larger, -1 if v1 is smallertemplate int compare(const T &v1, const T &v2){ if(v1 #include using namespace std;// implement strcmp-like generic compare function// returns 0 if the values are equal, 1 if v1 is larger, -1 if v1 is smallertemplate int compare(const T &v1, const T &v2){ if(v1 #include using namespace std;// 声明// ok: inline specifier follows template parameter list template inline int compare(const T&, const T&);// error: incorrect placement of inline specifier//inline template int compare(const T&, const T&);// 定义template int compare(const T &v1, const T &v2){ if(v1 class Queue{ public: Queue(); // default constructor Type &front(); // return element from head of Queue const Type &front()const; void push(const Type &); // add element to back of Queue void pop(); // remove element from head of Queue bool empty()const; // true if no elements in the Queue private: // ...};// 使用类模板Queue qi; // Queue that holds intsQueue< vector > qc; // Queue that holds vectors of doublesQueue qs; // Queue that holds strings// 非类型模板形参 #include #include using namespace std;// initialize elements of an array to zerotemplate void array_init(T (&parm)[N]){ for(size_t i = 0; i != N; ++i) { parm[i] = 0; }}int main(){ int x[42]; double y[10]; array_init(x); // instantiates array_init(int(&)[42] array_init(y); // instantiates array_init(double(&)[10]}/*编写泛型代码的两个重要原则: The parameters to the template are const references. 模板的形参是 const 引用。 The tests in the body use only < comparisons. 函数体中的测试只用 < 比较。*///16.2 实例化 --------------------------------------------------------------------------------------------------------// 实参类型不同,将产生编译错误 #include #include using namespace std;template int compare(const T &v1, const T &v2){ if(v1 #include using namespace std;// 定义成两个类型template int compare(const T &v1, const B &v2){ if(v1 T fobj(T, T); // arguments are copiedtemplate T fref(const T &, const T &); // reference argumentsstring s1("a value");const string s2("another value");fobj(s1, s2); // ok: calls f(string, string), const is ignoredfref(s1, s2); // ok: non const object s1 converted to const referenceint a[10], b[42];fobj(a, b); // ok: calls f(int*, int*)fref(a, b); // error: array types don't match; arguments aren't converted to pointers// in book #include #include using namespace std;template Type sum(const Type &op1, int op2){ return op1 + op2;} int main(){ double d = 3.14; string s1("hiya"), s2(" world"); sum(1024, d); // ok: instantiates sum(int, int), converts d to int sum(1.4, d); // ok: instantiates sum(double, int), converts d to int // sum(s1, s2); // error: s2 cannot be converted to int}// 函数指针template int compare(const T&, const T&);// pf1 points to the instantiation int compare (const int&, const int&)int (*pf1) (const int&, const int&) = compare; #include #include using namespace std;template T1 sum(const T2 &i, const T3 &j){ return i+j;}int main(){ long val3 = sum (2, 3); cout << val3 << endl; cout << sizeof(val3) << endl; return 0;}//16.3 模板编译模型 --------------------------------------------------------------------------------------------------------//16.4 类模板成员 --------------------------------------------------------------------------------------------------------#include #include using namespace std;template class QueueItem{ // private class: no public section QueueItem(const Type &t): item(t), next(0){} Type item; // value stored in this element QueueItem *next; // pointer to next element in the Queue};template class Queue{ public: // empty Queue Queue(): head(0), tail(0){} // copy control to manage pointers to QueueItems in the Queue Queue(const Queue &Q): head(0), tail(0) { copy_elems(Q); } Queue &operator = (const Queue &); ~Queue() { destroy(); } // return element from head of Queue // unchecked operation: front on an empty Queue is undefined Type &front() { return head->item; } const Type &front()const { return head->item; } void push(const Type &); // add element to back of Queue void pop(); // remove element from head of Queue bool empty()const { // true if no elements in the Queue return head == 0; } private: QueueItem < Type > *head; // pointer to first element in Queue QueueItem < Type > *tail; // pointer to last element in Queue // utility functions used by copy constructor, assignment, and destructor void destroy(); // delete all the elements void copy_elems(const Queue &); // copy elements from parameter};template void Queue ::destroy(){ while(!empty()) pop();} template void Queue ::pop(){ // pop is unchecked: Popping off an empty Queue is undefined QueueItem *p = head; // keep pointer to head so we can delete it head = head->next; // head now points to next element delete p; // delete old head element} template void Queue ::push(const Type &val){ // allocate a new QueueItem object QueueItem *pt = new QueueItem (val); // put item onto existing queue if(empty()) head = tail = pt; // the queue now has only one element else { tail->next = pt; // add new element to end of the queue tail = pt; }}template void Queue ::copy_elems(const Queue &orig){ // copy elements from orig into this Queue // loop stops when pt == 0, which happens when we reach orig.tail for(QueueItem *pt = orig.head; pt; pt = pt->next) push(pt->item); // copy the element} int main(){ return 0;}// 后面部分先略。//16.5 一个泛型句柄类 --------------------------------------------------------------------------------------------------------//16.6 模板特化 --------------------------------------------------------------------------------------------------------//16.7 重载与函数模板 --------------------------------------------------------------------------------------------------------// 后面两章,纯粹浏览,代码木有,或者先略。。。141118