const int NUMB = 4;
int n[] = {5,6,7,8};
// create a vector of strings using the n[] array
vector<int> partnums(n, n + NUMB);
The class functions vector name(src.begin, src.end)
Create a vector initialized with
elements from a source container
beginning at src.begin and ending at
scr.end
According to the book,
The vector partnums is declared as a
vector type int and initialized with
elements from the n array, starting
with the first array element n[0], and
ending with the last array element,
located at position n + NUMB.
I don’t get it, still. “Located at position n+ NUMB, isn’t the index starts at 0?
Or the compiler knows that this src.end is referring to position 1 (scr.begin), and count that from that position in the array n, and count to the 4th position)?
Thank you
The C++ standard library uses a convention that the ‘end’ iterator is actually referring to one element past the end, so in your case ‘begin’ would be the 0th position and ‘end’ the fourth (not third) position.
What is confusing in your citation above is that
n + NUMBis referred to as the last element in the array, which is incorrect. It is the (fictional) element after the last element in the array and simply used as an end marker.