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。’