I have some simple syntax questions about constructing a vector from an array. I just read Narue's "C and C++ timesaving tips and tricks" sticky. And I have come across something that I have seen elsewhere that I didn't really understand, and that is the following code assigns integers from an array to a vector using a form of the vector constructor that uses iterators. The thing that is bothering me is how does the compiler determine the starting point of the iteration based on the name of the array alone? The example Narue gave is something like this.

#include <iostream>
#include <vector>
using namespace std;

int main()
{
int a [] = {1,2,3,4,5};
vector<int> v (a, a+5);
cout << "The contents of v are:" ;
for( i = 0; i < v.size(); i++)
cout << " " << v[i];
cout << endl;
return 0;
}

I guess my question is, what is the initial number starting place for the array assignment? Meaning does it start counting at 1 or 0? The reason I am having a problem with this, is because the tutorial that I am reading from states that:
Input iterators to the initial and final positions in a sequence. The range used is [first,last], which includes all the elements between first and last, including the element pointed by first but not the element pointed by last. The function template type can be any type of input iterator.
Based on the previous statements from the tutorial, which may or may not be true, if a[0] which is the starting point and a[0+5] is the ending point, wouldn't previous code snipet exclude the last integer from the array in the vector construction? The only thing I can figure is that it starts counting at 1 instead of 0 and this just confuses me as to why array notation would start at 0 and vector notation starts at 1?

>>The only thing I can figure is that it starts counting at 1 instead of 0 a
No, arrays are always numberd beginning with 0. The a+5 in line 9 that you posted is 1 element beyond the end of the array, which satisfies the end statement that you posted. So it will fill array element numbers 0, 1, 2, 3, and 4 but not 5.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.