3

Imagine I have two vectors:

std::vector<int> A,B;
//Push a bunch of data into A
//Push a bunch of data into B

For whatever reason, I want to create an interface to these vectors such as follows:

packed combined(A,B);
for(packed::iterator i=combined.begin();i!=combined.end();++i)
  *i+=1;

This will have the same effect as:

for(std::vector::iterator i=A.begin();i!=A.end();++i)
  *i+=1;
for(std::vector::iterator i=B.begin();i!=B.end();++i)
  *i+=1;

I could code up a class to do this, but it seems like the code may already exist in a library somewhere. Does anyone know if this is the case?

Alternatively, can you think of a cunning way to do this?

Richard
  • 56,349
  • 34
  • 180
  • 251

1 Answers1

5

boost::join:

#include <vector>
#include <boost/range/join.hpp>

int main()
{
    std::vector<int> a = {1,2,3}, b = {4,5,6};
    for(int& i : boost::join(a, b)) {
        i += 1;
    }
}
Jesse Good
  • 50,901
  • 14
  • 124
  • 166