Associative Containers and the Algorithms

Rate this post

C++ Primer 4/e在Revisiting Iterators這個地方有一個關鍵觀念:『Although the map and set types provide bidirectional iterators, we can use only a subset of the algorithms on associative containers. The problem is that the key in an associative container is const. Hence, any algorithm that writes to elements in the sequence cannot be used on an associative container. We may use iterators bound to associative containers only to supply arguments that will be read.』

中文版的這樣寫:『雖然map 和set都提供bidirectional iterators,但我們只能在關聯式容器身上使用一部分演算法。問題在於關聯式容器的key是const。因此「對序列元素進行塗寫」的演算法不能用於關聯式容器。「綁定至關聯式容器的iterators」只能被演算法拿來作為讀取元素之用。』

另外有一個技巧:『When dealing with the algorithms, it is best to think of the iterators on associative containers as if they were input iterators that also support decrement, not as full bidirectional iterators.』

中譯:『與演算法「交手」時,最好把關聯式容器的iterators想成「一個 input iterator並支援decrement(–)運算子」,不要把它們想像成為全然的bidirectional iterators。』

這個我沒有特別的認知及感覺,純紀錄。

本站部分內容包含聯盟行銷連結。當您透過連結購買商品或服務時,本站將獲得微薄的佣金,這並不會影響您的購買價格,但能支持本站的運作,感謝您的支持。問題詢問

點我分享到Facebook

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *