Hi,
I have an vector of objects
and now I want to make a vector of Class *, which index pointing to a element in v1. for exampleCode:std::vector<Class> v1;
I can easily do this by writing the following loopCode:std::vector<Class *> v2; v2[0] = &v1[0]
But I wanted to do this in a more STL style way, so I used a transform:Code:v2.reserve ( v1.size() ); for (vector<Class>::iterator it = v1.begin(); it != v1.end(); it++) { v2.push_back ( &(*it) ); }
However, the addressof unary functor doesn't exist, so I coded one like so:Code:transform (v1.begin(), v1.territories.end(), v2.begin(), addressof<Class>() );
Now my two questions are,Code:template <class T> struct addressof : std::unary_function <T,T> { T* operator() (T& x) const {return &x;} };
1) Does a functor like addressof already exist?
2) Is there anything wrong/weird with my approach?
I wanted to create an array of Class *, so I could quickly shuffle the array without copying the class instance many times. e.g.
I know that my v2 array will contain invalid pointers if v1 changes, however, once v1 is setup it will never grow/shrink.Code:random_shuffle ( v2.begin(), v2.end() );
thanks
Andrew




Reply With Quote
