Assuming I have a class A
and a vector of instances instances
class A {
public:
int a;
}
std::vector<A> instances; // Contains several A objects
I now want to create a vector of int
from the attribute a
of objects in A
.
The naive way would be
std::vector attributes;
for (auto& obj: instances) {
attributes.push_back(obj.a);
}
Is there a way to do it in just one line? Something conceptually close to python's
attributes = [obj.a for obj in instances]