Is there any shortcut to convert from std::vector<T> to std::vector<T*> or std::vector<T&>?
Essentially I want to replace:
std::vector<T> source; std::vector<T*> target; for(auto it = source.begin(); it != source.end(); it++) { target.push_back(&(*it)); } with a single line.
To provide some context: I have one set of functions which do their computation on a std::vector<Polygon> and some which require std::vector<Polygon*>. So I need to convert back and forth a couple of times, because the interface of these functions is not supposed to change.
target.reserve(source.size())to avoid unnecessary memory allocations.targetwill contain pointers to the objects stored insource. Ifsourcechanges (i.e. an element is added), then the whole ofsourcecould be moved. It will be alright if you make sure that you regeneratetargetevery timesourcechanges.std::vector<T*>, since nobody changessourcewhile I call these, everything should be fine.