STL Pass iterators vs containers

c++, stl

Solution

If you can use the iterator form, then do. That allows the caller more flexibility - for example, they can pass reverse iterators, or some special purpose iterators that send values elsewhere, perform locking or whatever's needed. If you need more than just iterator access though - for example, to call `.size()` - passing the `Container` may be a better choice.

Problem

If I am writing a library function which needs to take in several containers, which is better? ``` template <class Iterator1, class Iterator2> void func_iterators(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2) ``` or ``` template <class Container1, class Container2> void func_containers(const Container1 &c1, const Container2 &c2) ```

Original source