My code is,
//Pointer to a Vector
std::vector<int> *ptr = new std::vector<int>;
ptr->push_back(20);
ptr->push_back(30);
ptr->push_back(40);
std::vector<int>::const_iterator pend = ptr->end();
for(std::vector<int>::const_iterator it = ptr->begin(); it != pend; ++it){
cout<<*it<<endl;
}
ptr->clear();
delete ptr;
pend = ptr->end();
for(std::vector<int>::const_iterator it = ptr->begin(); it != pend; ++it){
cout<<*it<<endl;
}
//Normal Vector
std::vector<int> nptr= {20,30,40};
std::vector<int>::const_iterator end = nptr.end();
for(std::vector<int>::const_iterator it = nptr.begin(); it != end; ++it){
cout<<*it<<endl;
}
nptr.clear();
end = nptr.end();
for(std::vector<int>::const_iterator it = nptr.begin(); it != end; ++it){
cout<<*it<<endl;
}
In the above sample code, I am iterating the pointer to vector and vector containers before and after clearing the container. In the normal vector case, the begin and end pointers are denoting the same element after done clear, it means the container is empty. In the pointer to a vector, the begin and end pointer never reset after using clear and delete the memory associated with the vector.
The output of the above code,
//pointer to vector before clear
20
30
40
//pointer to vector after clear
29006928
0
33
0
//vector before clear
20
30
40
//vector after clear
**no output**