C++STL基础及应用第27页
来源:互联网 发布:java maven 配置 编辑:程序博客网 时间:2024/05/18 02:10
C++STL基础与应用第27页中的代码,如下:
#include <iostream>#include <iterator>#include <ostream>using namespace std;template<class T>class MyLink{public: struct Unit { T value; Unit * next; }; class LinkIterator { Unit * init; public: LinkIterator(Unit *init) { this->init= init; } bool operator !=(LinkIterator& t) { return this->init!=t.init; } void operator ++(int) { init = init->next; } Unit operator * () { return *init; } }; Unit * head; Unit * tail; Unit * prev; public: MyLink() { head = tail = prev = NULL; } void add(T & value) { Unit * u = new Unit(); u->value = value; u->next = NULL; if(head == NULL) { head = u; prev = u; } else { prev->next = u; prev = u; } tail = u->next; } Unit * Begin() { return head; } Unit * End() { return tail; } virtual ~MyLink() { if(head != NULL) { Unit * prev = head; Unit * next = NULL; while(prev != tail) { next = prev->next; delete prev; prev = next; } } } };//template <class T> //这句不能用//ostream & operator << (ostream & os, MyLink<T>::LinkIterator s)//这句也要改成如下ostream & operator << (ostream & os, MyLink<int>::LinkIterator s){ os << (*s).value ; return os;}template<class Init>void display(Init S,Init E){ cout << endl; for(Init mid = S; mid!=E; mid++) { cout << mid <<"\t"; } cout <<endl;}int main(int argc, char *argv[]){ int m=0; MyLink<int> ml; for(int i=0;i<5;i++) { m = i+1; ml.add(m); } MyLink<int> ::LinkIterator s(ml.Begin()); MyLink<int> ::LinkIterator e(ml.End()); display(s,e); return 0;}
0 0
- C++STL基础及应用第27页
- C++STL基础与应用(第95页)
- 《C++ STL基础 及应用》笔记
- C++ STL 基础及应用(0) 目录
- C++ STL 基础及应用(3) 迭代器
- C++ STL 基础及应用(5) 字符串
- C++ STL 基础及应用(6) 容器
- C++ STL 基础及应用(8) 算法
- C++ STL 基础及应用(4) 输出输出流
- C++ STL 基础及应用(7) 函数对象(仿函数)
- C++STL技术原理浅析与应用基础之vector
- C++ STL 基础及应用(1) STL历史、组成和使用方法
- C++STL基础
- STL-set,map基础应用
- c基础第7课-数组及简单字符串操作
- C++ STL 基础及应用(2) 模板与操作符重载
- C语言基础—函数的理解及应用
- C/C++语言基础 STL基础
- Android系统设置— android.provider.Settings
- junit java mock分享链接
- 奉节做前列腺手术要多少费用
- Fedora20安装TeXlive2014出现MD5错误的解决办法
- Android 高仿微信6.0主界面 带你玩转切换图标变色
- C++STL基础及应用第27页
- 比尔·盖茨和乔布斯都是从小公司起步的
- ubuntu14.04远程桌面连接
- iOS 获得当前连接的wifi的名字
- 解决 [IE] 中下载Excel 中闪隐的问题
- hdu-2097-Sky数
- Java的图片处理工具类
- 资源向导之" CSAPP "
- Hibernate Mapping Declaration——之 一