When a std::vector is deallocated, in which order does it free its elements
This can be checked using this example code :
#include <iostream>
#include <vector>
using std::cout, std::endl;
using std::vector;
bool printMsg = false;
class Object {
public:
~Object() {
if (printMsg) cout << "Destructing Object at " << this << endl;
}
};
int main() {
vector<Object> list;
{
Object o1;
Object o2;
Object o3;
cout << "Creating list ..." << endl;
list.push_back(o1);
list.push_back(o2);
list.push_back(o3);
}
printMsg = true;
cout << "Vector is full :" << endl;
cout << " -- first object at " << &list[0] << endl;
cout << " -- second object at " << &list[1] << endl;
cout << " -- third object at " << &list[2] << endl;
cout << "End of program, dealocating vector ..." << endl;
return 0;
}
which returns something like :
Creating list ...
Vector is full :
-- first object at 0x55d363e962c0
-- second object at 0x55d363e962c1
-- third object at 0x55d363e962c2
End of program, dealocating vector ...
Destructing Object at 0x55d363e962c0
Destructing Object at 0x55d363e962c1
Destructing Object at 0x55d363e962c2