STL template

V

Victor Bazarov

Lieven said:
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
}

It probably would be easier to declare it

template<class C> C test(C c)
{
// do stuff with c
}

std::vector and std::set have different number of template arguments
(and it's not 1, either).

V
 
C

Cy Edmunds

Lieven said:
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> &);
 
J

John Harrison

Lieven said:
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
}

Contents is unnecessary

All STL containers define value_type which is the type of the contained
elements. E,g,

template<class C>
C test(const C& c)
{
typename C::value_type v = c.front();
...
}

john
 
L

Lieven

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
}
 
J

John Harrison

The purpose of this is, to write a generic quicksort over stl-containers. I
know there is sort defined on them. But we have to parallelize an
algorithm for school and test the speedup. Would it then still be better to
pass iterators around?

Yes, and of course that's how the standard sort work.

In this context one advantage of using iterators is that you can then your
code on good old arrays, since pointers are iterators too.
 
M

Malcolm Smith

Because different containers have a different number of template arguments
have you considered using a struct with static member functions and then use
a combination of template template parameters and method overloading/partial
specialization.

I haven't considered this deeply but these are my initial thoughts.

If you don't have any luck yell out and I'll see if I can whip something up
to see if it works.

I've just about finished a TManagedContainer that uses any of the STL
containers in an aggregated fashion. Part of my design had to cater for the
differences between set, map and the other types. I used a similar approach
to the above technique.

--
Malcolm Smith
MJ Freelancing
http://www.mjfreelancing.com
Borland Technology Partner

Contributing Editor
C++Builder Developers Journal
http://bcbjournal.org
 
L

Lieven

Cy said:
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> &);
The purpose of this is, to write a generic quicksort over stl-containers. I
know there is  sort defined on them. But we have to parallelize an
algorithm for school and test the speedup. Would it then still be better to
pass iterators around?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top