vector of struct of vector, clear() issue
Apr 19, 2018 at 2:22pm UTC
Hello all,
If I define the following class:
1 2 3 4 5 6 7
class MyClass{
struct myItem {
int myInt;
vector<string> myStr;
}
vector<myItem> MyVector;
}
When I call MyVector.clear(), it will handle everything for me or I need to do it myself as follow:
1 2 3
for (auto & A:WorkingClass.MyVector)
A.clear();
WorkingClass.MyVector.clear();
Thanks.
Regds
LAM Chi-fung
Apr 19, 2018 at 2:34pm UTC
MyVector.clear() is enough.
Apr 19, 2018 at 2:39pm UTC
Calling clear on a MyClass::MyVector will call destructors on all of its elements (which call dtors on their elements...) so the contained strings and vectors will delete themselves.
Apr 19, 2018 at 2:49pm UTC
@Peter87 & @tpb, thanks for the clarification.
Topic archived. No new replies allowed.