Thomas Tutone wrote:
> zl2k wrote:
>
>
> > I have some questions about stl vector.
> > Suppose I have
> >
> > std::vector<std::vector<int> > v;
> > //populate v
> > //now I want to clear v, should I just write:
> > v.clear();
>
> Yes.
>
> > //or
> > for (int i = 0; i < v.size(); i++)
> > v[i].clear();
>
> Not necessary - Each component vector's destructor will clear that
> vector automatically (after all, that's what destruction is all about).
>
> > v.clear();
>
> Still have to do this, though.
>
> > What if I have a vector of objects, say
> > std::vector<boost::shared_ptr<obj> > v;
> > //populate v
> > //now I want to clear v, I guess I have to do
> > for (int i = 0; i < v.size(); i++)
> > v[i].reset();
>
> Again, not necessary - the whole point of smart pointers like
> boost::shared_ptr is to do this work for you.
>
> > v.clear();
>
> That takes care of it.
>
> Best regards,
>
> Tom
Thanks, Tom.
If I want to delete the vector, do I just say v.~vector(); without
specifying the deleting of its contents in the above case? But, If I
have vector<myobject*>, then I have to take care of each of them when
do clear and delete, right?
Have a good weekend.
zl2k
|