This states I can construct a vector from an array as follows:
// the iterator constructor can be used to construct from arrays:
int myints[] = {16,2,77,29};
vector<int> myvector (myints, myints + sizeof(myints) / sizeof(int) );
Why is the constructor’s second argument myints + sizeof(myints) / sizeof(int)?
The expression
sizeof(myints) / sizeof(int)gets the number of elements in themyintsarray.myintsgets a pointer to the first element of the array.So the expression
myints + sizeof(myints) / sizeof(int)is a pointer one past the end of themyintsarray, which is what the two-iterator constructor ofstd::vectorexpects. This creates a vector with a copy of all the elements in the original array.Bear in mind that pointers are iterators, so the above is equivalent to