c++ - How to set an std::vector with another one, where the two are vectors of different classes? -
is there way set std::vector one, 2 vectors of different classes?
struct { 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 optimise: for(size_t = 0; < va.size(); ++i) { vb.push_back(b(va[i].a, commonb)); }
anything (pseudo-code):
vb = va;
with b::b values uninitialised?
i'd point out, since range-based loops, haven't had desire use simple algorithms transform often. hope can see why.
std::transform(va.begin(), va.end(), std::back_inserter(vb), [commonb](a x) -> b { return b(x.a, commonb); }); (auto& e : va) vb.emplace_back(e.a, commonb);
Comments
Post a Comment