c++ - What does"one-past-the-last-element" mean in vectors? -
i'm learning vectors , confused on how array copying thevector here
double p[] = {1, 2, 3, 4, 5}; std::vector<double> a(p, p+5);
i know std::vector<double> a(3,5);
means `make room 3 , initialize them 5. how above code work?
the second point read paragraph copied above code.
understanding second point crucial when working vectors or other standard containers. controlled sequence expressed in terms of [first, one-past-last)—not ctors, every function operates on range of elements.
i don't know meant [first, one-past-last)
? know mathematically don't know why/how vector copy array in way?
edited
another related question
the member function
end()
returns iterator "points"one-past-the-last-element
in sequence. note dereferencing iterator returnedend()
illegal , has undefined results.
can explain one-past-the-last-element
it? , why?
never dereference end()
or rend()
stl containers, not point valid elements.
picture below can visualize this.
the advantage of half open range is:
1. handling of empty ranges occur when begin() == end()
2. iterating on elements can intuitively done checking until iterator equals end().
Comments
Post a Comment