How to set an std::vector with another one, where the two are vectors of different classes?

Viewed 115

Is there a way to set an std::vector with another one, where the two are vectors of different classes?

struct A {
   int a;
};

struct B {
   int a, b;
   B(int _a, int _b) : a(_a), b(_b) {}
};

int commonB = 123;

std::vector<A> va;
std::vector<B> vb;

// Fill va...

// Code I would like to optimise:
for(size_t i = 0; i < va.size(); ++i) {
    vb.push_back(B(va[i].a, commonB));
}

Anything like (pseudo-code):

vb = va;

with the B::b values uninitialised?

4 Answers
Related