Sign in to follow this  
Liam M

C++: Generic, cross container forward iterator?

Recommended Posts

I'm trying to find a generic, cross container, forward iterator such that I can write code that resembles:


void function f(iterator i)
{
...
}

f(some_std_vector.begin());
f(some_std_list.begin());





Where some_vector and some_list will always be containers storing the same type. I don't anticipate the need for associative container (std::map) compatibility.

Pretty vague, but hopefully you guys get the idea. The obvious solution is to use templates, and I would, but doing so would require me to modify someone elses code, and that could quickly become a much bigger pain in the butt.

Any ideas?

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
I can't think of a solution offhand that wouldn't require modifying the other code...


The best I can think of at the moment is to write a method for each container type, which loads the data into some standard container, which then gets passed into another method which does all the work. This seems like overkill, and a tad inefficient. The other option to just pick a container and live with it, and another is to write a whole heap of duplicate code.

I was sort of hoping there's some standard, quicker way of doing this that doesn't require me to write a heap of redundant code, or lock into one container. Things aren't looking up, huh?

[Edited by - Liam M on October 12, 2010 7:02:07 AM]

Share this post


Link to post
Share on other sites
Can you explain exactly how you would need to modify the other code? Why can't you just do:

template <typename IteratorT>
void foo(IteratorT begin, IteratorT end)
{
for(IteratorT iter = begin; iter != end; ++iter)
iter->Frobnicate();
}



This sort of thing is done all over the standard library and works fine...

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
Can you explain exactly how you would need to modify the other code? Why can't you just do:


In a nutshell, because foo is a pure virtual method. The problem with modifying the code is that the library I'm modifying is built to be very compatible, and largely resemble, Nokia Qt. If I need to modify code external to my own, I need to go through various layers of management, and they won't like the necessary changes because it makes their product slightly less 'Qt like'. Additionally, if I were to modify the class such that the method was no longer virtual, that would also require me to involve management because it's non-standard behavior.

Quote:
Original post by ApochPiQ
This sort of thing is done all over the standard library and works fine...


Oh don't get me wrong, my own code is riddled with similar code.

Share this post


Link to post
Share on other sites
Alright, I've decided to write a few extra methods which shift data into a standard container, and make the overloaded methods virtual. This gives me a bit of leverage, in that I can provide reasonable functionality for people who really don't care about speed, as well as allowing people who do care about speed, to have each method call a template method such as the one shown above by ApochPiQ to take care of business.

If anyone has a better solution, I'd love to hear about it. For now, I think the above should be adequate.

Thanks ApochPiQ.

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