Sign in to follow this  
StasB

[C++] Operate on std::list of arbitrary type?

Recommended Posts

Hi!
Is it possible to write a function that accepts any std::list as an argument, regardless of the type of its elements?
If not, what are the alternatives?
Any sort of help is very welcome. =)

Share this post


Link to post
Share on other sites
You can use templates:

template<typename T>
void function(const std::list<T> &list) {
// ...
// You may need to use typename for nested types, like iterators
}



Now, the C++ way to do this is to accept a pair of templated iterator types. This way it will work with (almost) any C++ standard container, and others that conform to its interface. It also allows you to work with sub-ranges.

template<typename Iterator>
void function(Iterator begin, Iterator end) {
// ...
}

Share this post


Link to post
Share on other sites
Thanks for your reply!
If I'm not mistaken, the first suggestion would leave me with no ability to iterate through my list and the second one would leave me with no ability to insert\erase elements. My function needs to be able to do both. I guess I could combine the two by using two template parameters. Well, I'm clearly no C++ expert... is that an acceptable solution?

Share this post


Link to post
Share on other sites
what about this?
you can iterate over the list and modify it
template <class T>
void func(std::list<T>& l, T val)
{
//example of usage,
for(std::list<T>::iterator it = l.begin(); it != l.end(); it++)
{
l.insert(it, val);
}
}

Share this post


Link to post
Share on other sites
Thanks for the replies, guys. =)

@SiCrane: I'm not sure I follow... Could I have a snippet of example code, please?
@blubberbert: That would be nice, but, as rip-off hints in his post, it won't compile.

Share this post


Link to post
Share on other sites
Quote:
Original post by StasB
@blubberbert: That would be nice, but, as rip-off hints in his post, it won't compile.


for (typename std::list<T>::iterator it = l.begin(); ...

Share this post


Link to post
Share on other sites
Ah, yes, that one works! I guess I have some reading to do on the subject of templates. =)
Thank you, guys.
On a slightly unrelated note... When I define my template in a separate file of the project and try to include it in Main.cpp, any changes I make to the template are ignored until changes are made to Main.cpp. I guess this behavior makes sense for interface headers, but it's clearly a problem for templates. Is this something specific to Dev-Cpp? What can I do about it?

Share this post


Link to post
Share on other sites
Heheh. People have been telling me that for years. I'm going to have a hard time switching to something else. I love this tool because it's so small and simple. Coupled with a few DevPacks on a USB drive, I could install it within minutes and start coding on any PC.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this