I intentionally made iterator to exceed the size of std::vector like,
for (std::vector <Face>::iterator f = face.begin();
f!=face.end()+5;
++f)
{
// here I try to access (*f).P
// note that I added 5 to the face.end()
}
I did not face any error neither during compiling and run-time. How can I prevent that?
If you want checked access to
vectorelements, you may use theatfunction which throwsstd::out_of_rangeexception in case of boundary violation. Example:The standard doesn’t specify any checked iterators. The wording in the standard is that access to an invalid iterator results in undefined behavior. But many implementations provide checked iterators. If portability is not a concern, you may use one of those checked iterators. For example, in MSVC Debug mode,
vector<T>::iteratoris a checked iterator. In Release mode, however, it’s just a typedef forT*