[cs23021] Should we pass iterators or vectors to functions?

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Nov 17 13:17:02 EST 2010


> For functions that manipulate vectors, should they have the vector itself as
> an argument or the vector's iterator as an argument? It seems like it is
> less efficient for functions to accept the vector instead of the vector's
> iterator because that function will need to recreate an iterator in order to
> manipulate the vector.

For first part of the vectors lab you do not have to create functions:
all code can go into main(). If you do have functions and are
concerned about efficiency of call-by-value passing of a vector (you
are right, a complete copy is created), then use pass-by-reference
with "const" as type modifier.

In the second part of the lab, the functions are member functions of
the object that contains vector as an attribute so the vector does not
need to be passed to the functions.

Thanks,
-- 
Mikhail


More information about the cs23021-2 mailing list