C++编程(E)

33 vector template class t class a allocatort class

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 而且,我们可以为各个 类选择最合适的表示方 式,而不去考虑其他容 器的情况 有关操作的实现基本上 是最优的 young@buptnet.edu.cn 专门化的容器和迭代器 对于大部分容器,一种公共的使用方式就是 迭代式地穿过整个容器,一个接一个地查看 元素,这通常可以通过定义适合于容器类型 的迭代器类做到 最理想的情况是:一个迭代类能够应用于各 种容器类 template<class T>class Itor { // 公共界面 public: virtual T* first() = 0; // 返回第一个元素的指针 virtual T* next() = 0; // 返回下一个元素的指针 }; Beijing University of Posts & Telecommunications Beijing young@buptnet.edu.cn 专门化的容器和迭代器 现在可以为Vector和List提供迭代器 template<class T>class Vector_itor:public Itor<T>{ Vector<T>& v; size_t index; public: Vector_itor(Vector<T>& vv):v(vv), index(0){} T* first(){ return (v.size())? &v[index=0]:0; } T* next(){ return (++index<v.size())? &v[index]:0;} }; template<class T>class List_itor:public Itor<T>{ List<T>& lst; List<T>::Link p; public: List_itor(List<T>&); T* first(); T* next(); }; Beijing University of Posts & Telecommunications Beijing young@buptnet.edu....
View Full Document

This note was uploaded on 02/10/2014 for the course ECE C++ taught by Professor Xiaoshengtang during the Fall '13 term at BUPT.

Ask a homework question - tutors are online