sy*******@gmail.com wrote:
But this is the same for "new" any class, not just STL containers. Am I
correctly?
I have a function which go thru a list of Rect ( list<Rect*>), call the
'area' attribute and put that in a vector. Is there a better way to
achieve what I want?
Here is what I did:
template<class T> struct get_area : public unary_function<T, void>
{
get_area(int size) { area = new vector<float>(size); }
void operator() (T x) {
area->push_back(x->area);
}
public:
vector<float>* area;
};
vector<float>* getArea(const RectList& rectList) {
int size = rectList.size();
get_area<Rect*> p = for_each(rectList.begin(), rectList.end(),
get_area<Rect*>( size ));
return p.area;
}
It is not necessarily wrong to call new for a vector type. In your
example, there are many dangers. Firstly you are returning a vector
created with new but the user is not calling new themselves and are
likely to forget that they must call delete. Presumably you are
expecting your user to store the result in a smart pointer, possibly
auto_ptr.
I would use std::transform rather than for_each by the way. And then I
don't think you really need a function to do it, but if so and you
don't want to rely on RVO then have your function take a reference to a
vector.
struct AreaTransform
{
float operator()( const Rect* pRect ) const
{
return pRect->area;
}
};
std::vector< float > vect; // as parameter std::vector<float> & if in a
function)
vect.reserve( rectList.size() ); // if in a function, possibly clear
the vector first too
std::transform( rectList.begin(), rectList.end(), std::back_inserter(
vect ), AreaTransform() );
// if in a function and relying on RVO you can simply return vect