I read some code written in c++ as following:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int iarr[] = {30, 12, 55, 31, 98, 11, 41, 80, 66, 21};
vector<int> ivector(iarr, iarr + 10);
}
in the above code, I pass iarr and iarr+10 to ivector(iarr, iarr + 10) to create a new vector, is this a proper way to construct a vector? I checked the STL document, it is not mentioned there, is this allowed?
and also, array iarr contains 10 elements, should I use ivector(iarr, iarr+9)?
Yes, it is allowed and yes, you are doing it right.
You are calling this templated constructor:
The template parameter
InputIteratorisint*(this is the type of the expressionsiarrandiarr + 10).Since the documentation states that
_Lastmust point to one element beyond the last in the range, the+ 10is also correct to copy all 10 elements in the array (iarr + 9points to the last element,iarr + 10points to one beyond the last element).