Is begin () == end () for any empty () vector?

I have long assumed that for any empty std::vector V, V.begin() == V.end() . However, I do not see anything in the C ++ specification, which states that this is always true. Is this necessarily true or is it true for most implementations?

+58
c ++ stdvector
Jul 22 '13 at 19:50
source share
4 answers

Yes, this is what the standard requires from it for empty() for any container.

ยง 23.2.1 Table 96 of the C ++ 11 standard states:

  +----------+---------------+----------------------+ |Expression| Return Type | Operational Semantics| |----------|---------------|----------------------| |a.empty() |Convertible |a.begin() == a.end() | | |to bool | | | | | | +-------------------------------------------------+ 
+79
Jul 22 '13 at 19:53 on
source share

23.2.1 General requirements for the container , in particular table 96 The requirements for the container are

a.empty() converted to bool , operational semantics a.begin() == a.end()

Then

6 begin() returns an iterator referring to the first element in the container. end() returns an iterator, which is the end value for the container. If the container is empty, then begin() == end();

(my accent)

+22
Jul 22 '13 at 19:54
source share

http://www.cplusplus.com/reference/vector/vector/end/

If the container is empty, end () matches begin ().

+1
Jul 22 '13 at 19:55
source share

Yes it's true. Here is the proof . And of course, std::distance(a.begin(), a.end()) == 0 for an empty vector.

0
Jul 22 '13 at 19:53 on
source share



All Articles