1 / 23

面向对象的程序设计 第三讲 <续>模板编程

面向对象的程序设计 第三讲 <续>模板编程. 华中科技大学 CAD 中心 吴义忠( 62920915 ) wuyz@hustcad.com. STL 容器类: vector<int> Stack 的改进. class Stack { public: bool push( const string& ); bool pop( string &elem ); bool peek( string &elem ); bool empty(); bool full();

osric
Télécharger la présentation

面向对象的程序设计 第三讲 <续>模板编程

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 面向对象的程序设计第三讲 <续>模板编程 华中科技大学CAD中心 吴义忠(62920915) wuyz@hustcad.com

  2. STL容器类:vector<int> Stack的改进 class Stack { public: bool push( const string& ); bool pop( string &elem ); bool peek( string &elem ); bool empty(); bool full(); // definition of size() is placed within class // other members are simply declared ... int size() { return _stack.size(); } private: vector<string> _stack; }; 回顾 template <typename valType> class Stack { public: bool push( const valType& ); bool pop( valType &elem ); bool peek( valType &elem ); bool empty(); bool full(); int size() { return _stack.size(); } private: vector<valType> _stack; };

  3. 模板定义的关键字 • 模板的定义 < X x1 >,X可以为: • typename 传入类型名 • class 传入类名 • template< > 模板嵌套 • int等具体类型,传入具体数 • 函数指针类型,传入函数指针

  4. 本章设计任务:二叉树操作 Binary tree • 每个节点最多只有一个父节点,最多只有两个子节点 • 只有一个根节点 • 二叉树操作:遍历(前序、中序、后序)、插入、删除、查找等

  5. 6.1 参数化的节点数据类型 • BTnode(节点类,负责存储数据和连接指针)类设计 • BineryTree(存储根节点,封装二叉树的各种操作)类

  6. 6.2 类模板定义

  7. 成员函数的定义 • 构造函数 • 一般成员函数

  8. 6.3 类型参数的处理 效率考虑 • 尽量使用const reference • 尽量避免二次赋值:构造之前,执行成员数据构造 :_val()

  9. 6.4 二叉树类模板的实现 • 插入新元素,规则:保持左小右大 算法: 1)如果等于节点值则其重复数加1; 2)如果插入值小于节点值,则插入其左节点(若空,则新建,否则递归); 3)否则插入其右节点

  10. 删除操作:保持原有排序特性 • 算法介绍: 1)根节点删除 2)一般节点删除 3)使一个节点成为子树的最左叶子节点 static

  11. 删除根节点 多余!

  12. 改进方法: 去除&, 传入this指针 加一句: BTnode *tmp = prev; 和 If (tmp->_val > val) tmp->-_rchild = prev; Else tmp->_lchild = prev; 删除一般节点

  13. 删除整个二叉树

  14. 二叉树遍历

  15. 6.5 函数模板实现output运算符 template <typename valType> inline void BTnode<valType>:: display_val( BTnode *pt, ostream &os ) const { os << pt->_val; if ( pt->_cnt > 1 ) os << "( " << pt->_cnt << " ) "; else os << ' '; } template <typename elemType> ostream& BinaryTree<elemType>:: print( ostream &os, void (BinaryTree::*traversal)( ostream& ) const ) const { (this->*traversal)( os ); return os; } template <typename valType> void BTnode<valType>:: inorder( BTnode *pt, ostream &os ) const { if ( pt ) { if ( pt->_lchild ) inorder( pt->_lchild, os ); display_val( pt, os ); if ( pt->_rchild ) inorder( pt->_rchild, os ); } }

  16. 6.6 非类型参数模板:常量表达式和默认参数

  17. num_sequence重写

  18. 函数地址作为参数

  19. 6.7 模板参数化设计策略 • LessThan()函数对象模板 • 潜在问题 如果elemType类型未提供 < 运算符,编译出错!

  20. LessThan改进 ltpi(1200); //return false lpts(“Boy”); //return true;

  21. 6.8 模板成员函数 • 优点: 不用多个重载函数或多个类,完成不同类型数据的处理

  22. 类模板中也可以使用member template function

  23. 作业 • pp134练习4.5 • pp89练习6.2

More Related