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

I study vectors and is confused by how the array copies in thevector here

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

I also know that std::vector<double> a(3,5); means "free up space for 3 and initialize them with 5. How does the above code work?

Secondly, I read the paragraph where I copied the above code.

Understanding the second point is crucial when working with vectors or any other standard containers. A controlled sequence is always expressed in terms of [first, one-last-last] - not only for ctors, but also for every function that works with a number of elements.

I don't know what [first, one-past-last) means? I know mathematically, but I don’t know why / how the vector copies the array in this way?

Edited

another related question

The end() member function 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?

+6
source share
4 answers

Never look for end() or rend() from STL containers, as they do not point to valid elements.
This snapshot below will help you visualize this.

enter image description here

Half-open range advantage:
1. Processing of empty ranges occurs when begin () == end ()
2. Iteration over the elements can be done intuitively by checking until the iterator is equal to end ().

+11
source

Strongly connected to containers (e.g. vector, list, map) is the concept of iterators. An iterator is an abstraction of a C ++ pointer. That is, an iterator points to an object inside the container (or one after the last element), and dereferencing an iterator means access to this element.

Take, for example, a vector of 4 elements:

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

The standard library of templates (algorithms in the standard) works on ranges, not on containers. Thus, you can apply operations not only to the entire container, but also to ranges (consecutive elements of the container).

The range is specified [first, last) (inclusively first, last - last). That's why you need an iterator to the end: specify a range equal to the entire contents of the container. But since this iterator points beyond, it is illegal to play it.

+3
source

The std::vector constructor has several overloads.

For std::vector<double> a(3,5); the fill constructor is used:

 explicit vector (size_type n); vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type()); 

This takes the size parameter as the first parameter and optional and the third parameter, the second parameter indicates the value that you want to give the newly created objects.

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

Uses another constructor overload, namely a range constructor:

 template <class InputIterator> vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); 

This takes the iterator to the beginning of the collection and the end() tetra and iterates around and adds to vector until first == last .

The reason end() implemented as one-past-the-last-element is because it allows implementations to check equality as:

 while(first != last) { //savely add value of first to vector ++first; } 
+2
source

Iterators are an abstraction of pointers.

The half-open interval [a,b) is defined as all elements x>=a and x<b . The advantage of this is that [a,a) well defined and empty for any a .

Everything that can be increased and compared can determine half the open interval. So [ptr1,ptr2) is the element ptr1 , then ptr1+1 , then ptr1+2 , until you reach ptr2 , but not including ptr2 .

For iterators, this is similar - except that we do not always have random access. Therefore, we are talking about next instead of +1 .

Pointers are still considered some kind of iterator.

A range of iterators or pointers "speaks" of the elements that it points to. Therefore, when a vector takes a pair of iterators (first and one after the end), this defines a half-open interval of iterators, which also defines the set of values ​​that they point to.

You can build a vector from such a half-open range. It copies poimtrd elements to vector.

0
source

All Articles