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]







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草
伺服器连线错误,造成您的不便还请多多包涵!
「赞助商连结」





like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP