Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stdvector

I have long assumed that for any empty std::vector V, V.begin() == V.end(). Yet I see nothing in the C++ specification that states this to always be true. Is it necessarily true or does it just happen to be true on most implementations?

like image 633
OldPeculier Avatar asked Jul 22 '13 19:07

OldPeculier


People also ask

What should begin () and end () do for a container?

vector::begin() function is a bidirectional iterator used to return an iterator pointing to the first element of the container. vector::end() function is a bidirectional iterator used to return an iterator pointing to the last element of the container.

What does vector begin return if vector is empty?

std::vector::begin Returns an iterator pointing to the first element in the vector. Notice that, unlike member vector::front, which returns a reference to the first element, this function returns a random access iterator pointing to it. If the container is empty, the returned iterator value shall not be dereferenced.

What is empty vector?

The empty vector control allows you to examine if the transfection reagents or the transfection process itself has any cytotoxic effects on the target cells. Another type of transfection control is an internal control vector, which measures transfection efficiency.

What is V begin () in vector?

The begin() function is used to return an iterator pointing to the first element of the vector while front() function is used to return a reference to the same element in the vector container.


1 Answers

Yes, that's what the standard requires it to be for empty() for any container.

§ 23.2.1 Table 96 of the C++11 standard says:

 +----------+---------------+----------------------+  |Expression|  Return Type  | Operational Semantics|  |----------|---------------|----------------------|  |a.empty() |Convertible    |a.begin() == a.end()  |  |          |to bool        |                      |  |          |               |                      |  +-------------------------------------------------+ 
like image 87
Rapptz Avatar answered Sep 20 '22 15:09

Rapptz