I have a function which needs to receive either an std::list
or an std::vector
of MyClass *
objects and do a bunch of processing depending on what's inside.
I don't want to duplicate the function body.
I only need to use these objects to iterate over them and do some read-only checks.
I had thought of passing .begin()
and .end()
iterators directly, but that doesn't seem pretty at all.
Is there a better way to get around this, currently I have the following solution (which creates another list out of the vector being passed, which is hardly ideal either).
void process(std::list<MyClass*> input)
{
//A lot of processing
BOOST_FOREACH(MyClass* itMyClass, input)
{
//some checks, creating new list based on the checks
}
//A lot of processing
}
void process(std::vector<MyClass*> input)
{
process(std::list<MyClass*>(input.begin(), input.end()));
}
EDIT:
It seems that many people are suggesting to go for begin()
and end()
after all, I've made it work in a way similar to the example below. Thanks for your help.
//This one is private
template <typename Iterator>
void process(Iterator begin, Iterator end)
{
//A lot of processing
for (; begin != end; ++begin)
{
//some checks, creating new list based on the checks
}
//A lot of processing
}
void process(std::list<MyClass*> input)
{
process(input.begin(), input.end());
}
void process(std::vector<MyClass*> input)
{
process(input.begin(), input.end());
}