C++ Primer 4/e在Generic Algorithms这个地方有一个关键观念:‘The generic algorithms do not themselves execute container operations. They operate solely in terms of iterators and iterator operations. The fact that the algorithms operate in terms of iterators and not container operations has a perhaps surprising but essential implication: When used on “ordinary” iterators, algorithms never change the size of the underlying container. As we’ll see, algorithms may change the values of the elements stored in the container, and they may move elements around within the container. They do not, however, ever add or remove elements directly.
As we’ll see in Section 11.3.1 (p.406), there is a special class of iterator, the inserters, that do more than traverse the sequence to which they are bound. When we assign to these iterators, they execute insert operations on the underlying container. When an algorithm operates on one of these iterators, the iterator may have the effect of adding elements to the container. The algorithm itself, however, never does so.’
中文版的这样写:‘泛型演算法不执行容器操作。他们只根据iterators和其相关操作来运作。这种“以iterators为基础”而非“以容器操作为基础”的事实,惊人(但十分贴近本质)地暗示:当使用一般的(ordinary)iterators时,演算法绝不会改变底层容器的大小。我们也将看到,演算法可能改变容器元素的值,可能在容器中移动元素,但都没有直接添加或移除元素。
我们将在11.3.1节(p.406)看到一种特殊的iterators:inserter,它在它所绑定的序列尚可进行比寻访更多的操作。一旦用上这种iterators,iterators本身会对容器添加元素,至于演算法本身从来不这么做。’
ㄝ!中英文的11.3.1都是在406页啊!突然注意到这个页数的问题,没想到侯捷先生的功力竟然可以翻到同一页上,佩服阿!
不过我还搞不清泛型眼算法跟容器的操作的差异,有待加强了解!