mardi 26 décembre 2017

How to check for std::next(x) pointing to a valid address in C++?

Let's say I have the following vector of std::string filled with data:

std::vector<std::string> japan;

And, I search for elements in the vector as follows:

std::string where;

auto found = std::find(japan.begin(), japan.end(), where);

My issue is that sometimes I need to check the next element in the vector with respect to the "found" one as follows:

std::string here = *std::next(found);

However, it is not always the case that there is something at the next iterator, and attempting to access such non-existing element gives me "Expression: vector iterator not dereferencable" runtime error message, which is understandable.

My question is, how do I check that std::next(found) is a valid address so that I don't raise said error?

Thank you.

Aucun commentaire:

Enregistrer un commentaire