stl - Get vector of pointers from vector in C++ -
is there easy way of creating vector of pointers elements of vector?
i.e. easier below
std::vector<t*> fn(std::vector<t> &v) { std::vector<t*> r; (int = 0; < v.size(); i++) { r.push_back(&v[i]); } return r; }
edit: incoming vector reference
as @benoit suggested, bad idea store these pointers. if really want it, can use std::transform
this:
template<class t> struct address { t* operator()(t& t) const { return &t; } }; template<class t> vector<t*> fn(vector<t>& v) { vector<t*> r; transform(v.begin(), v.end(), back_inserter(r), address<t>()); return r; } int main( void ) { vector<int> a; a.push_back(0); fn(a); }
Comments
Post a Comment