jiafu
jiafu

Reputation: 6556

When a string is "" or a vector has no elements, is `begin()` equal to `end()`?

When a std::string is "" or a vector has no elements, is begin() equal to end()?

If so, what the value of begin()/end()?

Upvotes: 0

Views: 264

Answers (2)

Drew Dormann
Drew Dormann

Reputation: 63889

When a std::string is "" or a vector has no elements, is begin() equal to end()?

Yes, for any empty standard library container, including std::string and std::vector, begin() will return the same iterator as end().

If so, what the value of begin()/end()?

It will be an iterator which is unique to that container, but should not be dereferenced. Doing so would cause undefined behavior.

Upvotes: 6

templatetypedef
templatetypedef

Reputation: 372972

Yes. Any STL container type with no elements will have begin() == end(), as the range of elements is the half-open range [begin(), end()).

Hope this helps!

Upvotes: 4

Related Questions