"Lieven" <li*************@hotmail.com> wrote in message
news:41*********************@news.skynet.be...
I want to make a function template that is generic over all of the
stl-containers. This function can take a vector, a set or stack as input
and as second argument the class of the contents of the container. No
mather what. Is this possible?
My C++ is a bit rusty so this is the best I came up with:
template<class C, class Contents>
C test(C<Contents> c){
//do stuff with c
}
I usually find it better to avoid passing a container as an argument.
Standard library (STL) style is to pass iterators instead:
// untested code
template <typename INITER, typename OUTITER>
INITER
copy(INITER first, INITER last, OUTITER out)
{
while (first != last)
*out++ = *in++;
return first;
}
for instance, will copy elements from just about any container (including
files) to just about any other container. This style takes a little getting
used to but actually offers better genericity than making a template out of
the container type and passing the container. Plus you have a whole standard
library full of algorithms to look at for examples.
If you need to pass a container you should usually pass a reference or const
reference to avoid copying the whole container:
void foo(const std::vector<double> &);
--
Cy
http://home.rochester.rr.com/cyhome/