看板 CompBook 關於我們 聯絡資訊
C++ Primer 答客問 (51) - 勘誤與疑問 侯捷 [email protected] 2000.05.18 第一次發表於 清大.楓橋驛站(140.114.87.5).電腦書訊版(Computer/CompBook) 本文將於日後整理於 侯捷網站/侯捷譯作/C++ Primer 中文版/答客問 侯捷網站:www.jjhou.com ---------------------------------------------------------------- zychang wrote (2000/05/12) : > 侯老師您好: > > 首先是勘誤方面: > p.1188 > swap_range() ==> swap_ranges() (要加 s 才對) 是的,謝謝。我將記錄於勘誤列表之中。摘錄如下: ■p1188, 小標題 swap_range()(原書錯誤) 原文:swap_range() 更正:swap_ranges() 注意:該小段的函式原型、文字第一行、文字第四行各有一個 swap_range(), 皆應改為 swap_ranges()。同時請修改 p.vii 之目錄及 p.1235 之索引。 感謝:zychang(張振宇先生) > 疑問與請益方面: > > p.1183 > set_union() 建構出一個排序過的序列... 這一段。 > > 事實是 set_union() 所需的兩個 containers 需先排序過, > set_union() 執行完的結果才有 ordering。 > 您這樣的譯筆是乎是說 set_union() 所需的兩個 containers > 不需先排序過,set_union() 執行完後就能將結果排序。 > 而您給的範例程式亦沒有強調這一點(即沒有 sort 的程式碼) 我同意你所說:set_union() 所接受的兩個 ranges 需先排過序。 (見 "Generic Programming and the STL" p.324) C++ Primer 原書在解釋 set_union() 時並未特別強調這一點, 所以我的譯文也就沒有出現這一點。 另我發現譯文有一處值得修潤。收錄於勘誤列表之中。摘錄如下: ■p1183, L6(譯筆重修) 原文:傳回值 OutputIerator 指向被放進 result 所指之 container 內的 最後元素的下一位置。 更正:傳回值 OutputIerator 指向「result 所指之 container」內的 最後元素的下一位置。 > p.1136 > equal_range() > 這個泛型演算法的文字說明與範例程式讓我摸不著頭緒。 > 我無法從而得知這個泛型演算法到底在做什麼... > 我是看了 Rogue Wave Standard C++ Library 的 Help 才看懂的 > 這與此書對此泛型演算法的文字說明差異很大 > 附上 Rogue Wave Standard C++ Library 的 Help 對 equal_range() > 的說明: > > ======================================================= > Determines the valid range for insertion of a value in a container. > > Description > > The equal_range algorithm performs a binary search on an > ordered container to determine where the element value can be > inserted without violating the container's ordering. The library > provides two versions of the algorithm. The first version uses the > less than operator (operator <) to search for the valid insertion > range, and assumes that the sequence was sorted using the less > than operator. The second version allows you to specify a > function object of type compare, and assumes that compare was > the function used to sort the sequence. The function object must > be a binary predicate. > > equal_range returns a pair of iterators, i and j that define a range > containing elements equivalent to value, i.e., the first and last > valid insertion points for value. If value is not an element in the > container, i and j are equal. Otherwise, > i will point to the first element not "less" than value, and j will > point to the first element greater than value. In the second > version, "less" is defined by the comparison object. Formally, > equal_range returns a sub-range [i, j) such that value can be > inserted at any iterator k within the range. Depending upon the > version of the algorithm used, k must satisfy one of the following > conditions: > > !(*k < value) && !(value < *k) > > or > > comp(*k,value) == false && comp(value, *k) == false > > equal_range performs at most 2 * log(last - first) + 1 > comparisons. > ======================================================================== 《C++ Primer》對於 equal_range() 的文字解釋,要求讀者 跳躍參考 lower_bound() 和 upper_bound() 的意義與用法,因此 的確比較不好理解。你能夠以 Rogue Wave 的 Help 做為輔助,極好。 > p.654 > 下方程式碼 > > ps_Screen pH = &Screen::_height; > ps_Screen pW = &Screen::_width; > > 我覺得怪怪的..... > _height 與 _width 是屬於 private data member; > 為何能在非 class scope (以此段程式碼來看,並非定義於 > class scope)內被 access 呢? 的確不行。作者 Lippman 有時候會偷懶(或疏忽)以 private data members 直接拿來說明「只有 public data members 才能做」的事。 > p.632 > 上方文字: > 只有宣告為const的member functions,才可以處理const object... > 以我於 BCB5 測試結果,const object 還是可以呼叫 non-const function > 不過 BCB 會給個警告: > Non-const function called for for const object > 因此我覺得這句話不完全成立 > (抱歉,我並不知道 C++ Standard 所定的規則為何,可否請 > 侯老師說明一下) 《C++ Primer》所說的即為 C++ standard 所說的 :) 這方面各家編譯器寬緊不一。BCC 和 GCC 都只給警告,VC6 則給錯誤。 以下是我的測試。 #01 // 測試:《C++ Primer 中文版》p.632 #02 // (1) const object can invoke const member function #03 // (2) const object can't invoke non-const member function #04 // (3) non-const object can invoke const member function #05 // (4) non-const object can invoke non-const member function #06 #07 #include <iostream> #08 using namespace std; #09 #10 class A { #11 public: #12 A() { _i = 1; } #13 void set(int i) { _i = i; } // non-const member function #14 int get() const { return _i; } // const member function #15 #16 private: #17 int _i; #18 }; #19 #20 int main() #21 { #22 A const ca; // const object #23 A a; // non-const object #24 #25 ca.set(7); // (a) #26 // VC6 error C2662: 'set' : cannot convert 'this' pointer from 'const class A' to 'class A &', Conversion loses qualifiers #27 // BCB4 Warning W8037 : Non-const function A::set(int) called for const object in function main() #28 // GCC warning: passing `const A' as `this' argument of `void A::set(int)' discards const #29 #30 cout << ca.get() << endl; // 7 in BCB4 and G++. #31 // 1 in VC6 if (a) is remarked off #32 a.set(5); #33 cout << a.get() << endl; // 5 #34 } > p.643 > 中間一段文字: > 必須被定義於class 定義區之外(譯註:上例(10) ) > > 事實上,在BCB5 中,沒有行號(10) const int Account::nameSize 這程式碼, > 是沒有任何error 與warning的 > Why?? 是否這是BCB自己所擴充的呢? > 謝謝您的解答... > Best Regards 「在 class body 內直接對 static const integral data member 設定初值」, 此性質此稱為 in-class initialization。 這是 C++ 很晚近的新特性。照理,既有此一規則,即不需要在 class body 之外再寫 static data member 的定義式(書中的 (10))。不過,這一點 各家編譯器表現不一。情況有點混亂。 我曾有一篇文章:C++ Primer 答客問 (27)【標準與實作之間】 可資參考。 -- the end  -- ※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ Mail: [email protected]