Here http://www.parashift.com/c++-faq/vector-is-contiguous.html is stated that vector.begin()
may not be equal to &vector[0]
. Why is it defined in this way. What does prevent vector.begin()
to be equal to &vector[0]
?
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.
The C++ function std::vector::end() returns an iterator which points to past-the-end element in the vector container. The past-the-end element is the theoretical element that would follow the last element in the vector.
To print all elements of a vector, we can use two functions 1) vector::begin() and vector::end() functions. vector::begin() function returns an iterator pointing to the first elements of the vector. vector::end() function returns an iterator point to past-the-end element of the vector.
An iterator for vector can be defined as some class. As member function returns the iterator then it is not necessary that it is a raw pointer to the first element of the array. It can be an object of that class. It is only required that the iterator would have defined operator *
that will return reference to the first element of a vector provided that the vector is not empty.
The iterator returned by vector.begin()
technically only lets you get at values via the dereference operator on it. That dereference operator could be doing all sorts of things inside. Most implementations simply store a pointer to a T
- or are a pointer to a T
- and then simply dereference it in their dereference operator, but that's not a requirement of the iterator concept.
See: Iterator
concept
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With