Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does"one-past-the-last-element" mean in vectors?

Tags:

c++

vector

I'm learning vectors and am confused on how the array is copying to thevector here

double p[] = {1, 2, 3, 4, 5};
std::vector<double> a(p, p+5);

I also know std::vector<double> a(3,5); means `make room for 3 and initialize them with 5. How does the above code work?

The second point is I read the paragraph from where I copied the above code.

Understanding the second point is crucial when working with vectors or any other standard containers. The controlled sequence is always expressed in terms of [first, one-past-last)—not only for ctors, but also for every function that operates on a range of elements.

I don't know what is the meant by [first, one-past-last) ? I know mathematically but don't know why/how vector copy the array in this way?

Edited

another related question

The member function end() returns an iterator that "points" to one-past-the-last-element in the sequence. Note that dereferencing the iterator returned by end() is illegal and has undefined results.

Can you explain this one-past-the-last-element what is it? and why?

like image 211
Asif Mushtaq Avatar asked Jan 16 '16 17:01

Asif Mushtaq


People also ask

How do you find the last element of a vector?

The immediate answer to your question as to fetching access to the last element in a vector can be accomplished using the back() member. Such as: int var = vec. back().

What is a past-the-end iterator?

std::vector::end Returns an iterator referring to the 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. It does not point to any element, and thus shall not be dereferenced.

How do you remove the last element of a vector?

The C++ function std::vector::pop_back() removes last element from vector and reduces size of vector by one.


2 Answers

Never dereference end() or rend() from STL containers, as they do not point to valid elements.
This picture below can help you visualize this.

enter image description here

The advantage of an half open range is:
1. Handling of empty ranges occur when begin() == end()
2. Iterating over the elements can be intuitively done by checking until the iterator equals end().

like image 184
Trevor Hickey Avatar answered Oct 10 '22 01:10

Trevor Hickey


Strongly coupled with containers (e.g. vector, list, map) is the concept of iterators. An iterator is a C++ abstraction of a pointer. I.e. an iterator points to an object inside the container (or to one past the last element), and dereferencing the iterator means accessing that element.

Lets take for instance a vector of 4 elements:

| 0 | 1 | 2 | 3 |  |
  ^           ^   ^
  |           |   |
  |           |   one past the end (outside of the container elements)
  |           last element           
  first element

The (algorithms in the) standard template library operate on ranges, rather than on containers. This way you can apply operations, not only to the entire container, but also to ranges (consecutive elements of the container).

A range is specified by [first, last) (inclusive first, exclusive last). That's why you need an iterator to one past the end: to specify a range equal to the entire contents of the container. But as that iterator points outside of it, it is illegal to dereference it.

like image 5
bolov Avatar answered Oct 10 '22 01:10

bolov