Anything like python's itertools.chain()?

W

workusenet1

I have a need for behavior like the chain() function in python's
itertools. It applies a composite pattern to iterators, allowing the
coder to iterate over multiple lists in sequence:
for i in itertools.chain([1, 2], [3, 4]): print i, # prints 1 2 3 4

I looked at boost's iterator library which is like python's itertools
in many respects, but unless I missed something it does not have a
parallel to chain().

What I'm hoping for is something like the following:

vector<int> a, b;
a.push_back(1); a.push_back(2);
b.push_back(3); b.push_back(4);
chain<vector<int>::iterator> ch;
ch.add_range(a.begin(), a.end());
ch.add_range(b.begin(), b.end());
for(chain<vector<int>::iterator>::iterator i = ch.begin(); i !=
ch.end(); ++ch)
{
std::cout << *ch;
}
// above prints 1234

I can probably roll my own, but I wanted to see if someone had already
produced this wheel before I reinvented it.

Thanks!
 

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
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top