Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequential sequence containers OR How to pack vectors

Tags:

c++

std

stl

boost

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?

like image 717
Richard Avatar asked Jun 27 '26 15:06

Richard


1 Answers

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;
    }
}
like image 156
Jesse Good Avatar answered Jun 29 '26 05:06

Jesse Good



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!