I have a C-array like this:
int X[]={0, 1, 2, 3, 4, 5, 6, 7, 8};
I need to create two stl vectors out of this array, by means of slicing, possibly sharing the highest amount of memory and making less deep copies possible.
The first vector Y must contain only the 0
-th and first element each three elements, for example in this case this new vector Y
will contain
std::vector<int> Y; // contains: [0,1,3,4,6,7]
and another vector Z must contain each 3rd element in the original array:
std::vector<int> Z; // contains [2,5,8]
A first solution based on for loops with copy is the following:
vector<int> Y,Z;
for (int i=0; i<9;i+=3)
{
Y.push_back(*(X+i));
Y.push_back(*(X+i+1));
Z.push_back(*(X+2));
}
but I'm pretty sure that by means of custom iterators the problem could have a more efficient solution. Is there, anyway, some other faster version of implementing a mask-view on this array that avoid generating copies?