a question about vector in c++
I read some code written in c++ as following:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
开发者_JAVA技巧
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:
template<class InputIterator>
vector(
InputIterator _First,
InputIterator _Last
);
The template parameter InputIterator
is int*
(this is the type of the expressions iarr
and iarr + 10
).
Since the documentation states that _Last
must point to one element beyond the last in the range, the + 10
is also correct to copy all 10 elements in the array (iarr + 9
points to the last element, iarr + 10
points to one beyond the last element).
Simple helper for arrays:
template <typename T, size_t N>
size_t size(T(&)[N]) { return N; }
template <typename T, size_t N>
T* begin(T(&array)[N]) { return array; }
template <typename T, size_t N>
T* end(T(&array)[N]) { return array + N; }
Now you can write:
int main() {
int const arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
std::vector<int> vec(begin(arr), end(arr));
}
And not worry about the size of the array any longer, it'll be computed automatically.
Yes, that is a constructor of std::vector
. It's this one:
template <class InputIterator>
vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
It takes two iterators (in your case pointers), one to the beginning and another to the end of the sequence of elements that are to initialize the vector. The last parameter is optional, and you don't need it unless you're using custom allocators.
The iterator to the end should be one past the last element you want to include. So, if you want all elements from iarr[0]
to iarr[9]
, you need to pass in iarr + 10
.
It is allocating a vector using Iterators and the original iarr[] shown. There are ten elements and +10 is a proper iteration because it is one step past the end. That's how vectors work - it must point to one position past the end. It is copying the contents of the array into the vector. More clearly, it is using this template to create the vector:
template <class InputIterator> vector ( InputIterator first,
InputIterator last, const Allocator& = Allocator() );
Iteration constructor: Iterates between first and last, setting a copy of each of the sequence of elements as the content of the container.
This code is indeed allowed, if you check for example the documentation here
template <class InputIterator>
vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
The range specified by the arguments follow the usual convention [first, last[, so passing iarr+10 is correct if you want the whole array to be copied
Yes, this is allowed, and ivector
will contain 10 elements. And no, it should not be 9
because the end iterator should be one step past the end. If you know what ranges are, that range would be represented by this range: [beginning, end)
. That is to say, include the first one, and go right up to but don't include the last one.
Because the STL (C++ standard library cough) is all templates, anything that supports the operators ++
and *
(dereference operator) can be passed as iterators to the vector constructor. This property amazingly makes the same code work for both pointers and vector
iterators. Standard library design at its best.
Yes, last line in function main() calls constructor of std::vector. Have a look here
to see all vector constructor overloads. The third one is used here. Its parameters are iterators for template type (template argument used here is int
and so iterator is of type int*
). Second argument is iterator that points to the first sequence element that will not be copied into vector.
精彩评论