📄 00000003.htm
字号:
<BR> ListPosition 的作用是:代表 List 里「现在的位置」,这样就允许许多位置 <BR> 并存於同一个串列中。ListPosition 是 List 的夥伴,所以 List 的内部可对 <BR> 外界隐藏起来(否则 List 的内部就会被它的公共运作行为所公开)。注意: <BR> ListPosition 可以把运算子多载起来,像是 advance()、backup(),因为运算 <BR> 子多载只是正常运作行为的语法糖衣而已。 <BR> <BR>[3] 把整个位置处理(iteration)当成是一个基元事件(atomic event),建一个 <BR> class template 去涵盖该事件。 <BR> <BR> 它不会在内部回圈中使用公共存取运作行为(它有可能是虚拟函数),所以效率 <BR> 能增进。不幸的,你的应用软体会多出些额外的二元码,因为 template 是以空 <BR> 间换取时间的。欲知详情,请见 [Koenig, "Templates as interfaces," <BR> JOOP, 4, 5 (Sept 91)], 以及 [Stroustrup, "The C++ Programming Language <BR> Second Edition," under "Comparator"]. <BR> <BR>======================================== <BR> <BR>Q117:「样版」(template)的用意是什麽? <BR> <BR>Template 本意是个压饼模子,它把饼乾都压成差不多一个样子(虽然饼乾的原料不 <BR>尽相同,但它们都有相同的基本外形)。同理,class template 是个样版模子,用 <BR>来描述如何将一系列的物件类别弄成同一个基本型式;而 function template 则是 <BR>用以描述一系列看起来差不多的函数。 <BR> <BR>Class template 常用於制造型别安全的容器(即使这仅止於「如何使用它」而已)。 <BR> <BR>======================================== <BR> <BR>Q118:"function template" 的语法/语意是什麽? <BR> <BR>考虑底下这个交换两个整数引数的函数: <BR> <BR> void swap(int& x, int& y) <BR> { <BR> int tmp = x; <BR> x = y; <BR> y = tmp; <BR> } <BR> <BR>假如我们想交换 float、long、String、Set 和 FileSystems,我们还得写那些 <BR>大致看起来都一样、只有型态不同的程式码,有够烦人。这种不花脑筋的重复性工作 <BR>,正是电脑的专长,於是我们想出了 function template: <BR> <BR> template<class T> <BR> void swap(T& x, T& y) <BR> { <BR> T tmp = x; <BR> x = y; <BR> y = tmp; <BR> } <BR> <BR>每次我们以一组型别来使用 "swap()",编译器会找到上面这定义,并造出另一个 <BR>"template function" ,来当作它的「案例」(instantiation)。譬如: <BR> <BR> main() <BR> { <BR> int i,j; /*...*/ swap(i,j); // 案例化 "int" 的 swap <BR> float a,b; /*...*/ swap(a,b); // 案例化 "float" 的 swap <BR> char c,d; /*...*/ swap(c,d); // 案例化 "char" 的 swap <BR> String s,t; /*...*/ swap(s,t); // 案例化 "String" 的 swap <BR> } <BR> <BR>(注意:"template function" 是 "function template" 实体化之後的案例。) <BR> <BR>======================================== <BR> <BR>Q119:"class template" 的语法/语意是什麽? <BR> <BR>考虑像是个整数阵列的容器类别: <BR> <BR> // 这会放在像是 "Array.h" 的标头档中 <BR> class Array { <BR> public: <BR> Array(int len=10) : len_(len), data_(new int[len]){} <BR> ~Array() { delete [] data_; } <BR> int len() const { return len_; } <BR> const int& operator[](int i) const { data_[check(i)]; } <BR> int& operator[](int i) { data_[check(i)]; } <BR> Array(const Array&); <BR> Array& operator= (const Array&); <BR> private: <BR> int len_; <BR> int* data_; <BR> int check(int i) const <BR> { if (i < 0 || i >= len_) throw BoundsViol("Array", i, len_); <BR> return i; } <BR> }; <BR> <BR>如同前述的 "swap()" ,一再为 float、char、String、Array-of-String 等等来重 <BR>复设计 Array 类别,是很烦人的。 <BR> <BR> // 这会放在像是 "Array.h" 的标头档中 <BR> template<class T> <BR> class Array { <BR> public: <BR> Array(int len=10) : len_(len), data_(new T[len]) { } <BR> ~Array() { delete [] data_; } <BR> int len() const { return len_; } <BR> const T& operator[](int i) const { data_[check(i)]; } <BR> T& operator[](int i) { data_[check(i)]; } <BR> Array(const Array<T>&); <BR> Array& operator= (const Array<T>&); <BR> private: <BR> int len_; <BR> T* data_; <BR> int check(int i) const <BR> { if (i < 0 || i >= len_) throw BoundsViol("Array", i, len_); <BR> return i; } <BR> }; <BR> <BR>不像 template function 那样,template classes(案例化的 class template)必 <BR>须将那些用来案例化的参数型态明示出来: <BR> <BR> main() <BR> { <BR> Array<int> ai; <BR> Array<float> af; <BR> Array<char*> ac; <BR> Array<String> as; <BR> Array< Array<int> > aai; <BR> } // ^^^-- 注意这空格;不要用 "Array<Array<int>>" <BR> // (编译器会把 ">>" 看成单一的元素) <BR> <BR>======================================== <BR> <BR>Q120:什麽是「参数化型别」(parameterized type)? <BR> <BR>另一种 "class template" 的说法。 <BR> <BR>「参数化型别」是一种型别,它被另一个型别或数值所参数化(parameterized)了。 <BR>像 List<int> 是一个型别 ("List") ,它被另一个型别 ("int") 所参数化。 <BR> <BR>======================================== <BR> <BR>Q121:「泛型」(genericity)是什麽? <BR> <BR>另一种 "class template" 的说法。 <BR> <BR>不要和「一般化」(generality,指不要过於特定的解题)弄混了,「泛型」指的是 <BR>class template。 <BR> <BR> <BR>======================= <BR>■□ 第20节:程式库 <BR>======================= <BR> <BR>Q122:怎样拿到 "STL"? <BR> <BR>"STL" 代表 "Standard Templates Library",标准模版程式库。取得法: <BR> <BR>STL HP official site: <A HREF="ftp://butler.hpl.hp.com/stl">ftp://butler.hpl.hp.com/stl</A> <BR>STL code alternate: <A HREF="ftp://ftp.cs.rpi.edu/stl">ftp://ftp.cs.rpi.edu/stl</A> <BR>STL code + examples: <A HREF="http://www.cs.rpi.edu/~musser/stl.html">http://www.cs.rpi.edu/~musser/stl.html</A> <BR> <BR>STL hacks for GCC-2.6.3 已经在 GNU libg++ 2.6.2.1 或更新版本里了(可能较早 <BR>的版本也有)。多谢 Mike Lindner。 <BR> <BR>======================================== <BR> <BR>Q123:怎样 ftp 到 "Numerical Recipes" 附的程式? <BR> <BR>它是用卖的,把它放到网路上散布是违法的。不过它只需 $30 美元而已。 <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -