Associative Containers and the Algorithms

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

發佈留言

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