In addition to the iterator checking described above, each container (except string) has a new member method:
bool invariants() const;
This method can be called at any time to assess the container's class invariants. If the method returns false, then the container has somehow become corrupted and there is a bug (most likely in client code, but anything is possible). If the method returns true, then no errors have been detected. This can easily be used in debug code like:
#include <vector> #include <cassert> int main() { int iarray[4]; std::vector<int> v(10); assert(v.invariants()); for (int i = 0; i <= 4; ++i) iarray[i] = 0; assert(v.invariants());
The for loop indexing over iarray goes one element too far and steps on the vector. The assert after the loop detects that the vector has been compromised and fires.
Be warned that the invariants method for some containers can have a significant computational expense, so this method is not advised for release code (nor are any of the debug facilities).