Passing std algorithm iterator parameters by value vs. by reference

前端 未结 4 1998
逝去的感伤
逝去的感伤 2021-02-19 04:45

I\'m wondering why in many template algorithms in the STL the arguments are not passed by reference but rather by value. Here is an example from the > h

4条回答
  •  时光说笑
    2021-02-19 04:57

    One thing that comes to my mind and which is against the constness in the reference: the iterators need to be modified when using them.

    Another implementation detail may be that iterators are really just implemented as pointers. So are references in most cases. If you pass the pointer by value, you copy it once but dereference it only when needed. If, however, the iterator-pointer itself is passed by a reference-pointer, then that has to be dereferenced first, just in order to get to the iterator, and that must be done each time the iterator is accessed. That is superfluous.

提交回复
热议问题