473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

STL template

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
}

Jul 22 '05 #1
7 1412
"Lieven" <li*************@hotmail.com> wrote...
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
Jul 22 '05 #2
"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/
Jul 22 '05 #3

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


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
Jul 22 '05 #4
Cy Edmunds wrote:
"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> &);

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?
Jul 22 '05 #5
> >
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.
Jul 22 '05 #6
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
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:IbZdd.299238$3l3.218782@attbi_s03...
"Lieven" <li*************@hotmail.com> wrote...
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

Jul 22 '05 #7
Now that I've read this extra bit of information, yes pass in iterators
instead.

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

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

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?

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.