Question about vector in C ++

I read the code written in C ++ as follows:

#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 the right way to build a vector ? I checked the STL document, it doesn’t mention there, is this allowed?

and also the iarr array contains 10 elements, should I use an ivector(iarr, iarr+9) ?

+8
c ++ arrays vector
source share
7 answers

Yes, it is allowed and yes, you are doing it right.

You invoke this template constructor:

 template<class InputIterator> vector( InputIterator _First, InputIterator _Last ); 

The InputIterator template InputIterator is int* (this is the type of the iarr and iarr + 10 expressions).

Since the documentation states that _Last should point to one element after the last in the range, + 10 also correct for copying all 10 elements in an array ( iarr + 9 indicates the last element, iarr + 10 points one after the last element).

+12
source share

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 don’t worry about the size of the array larger, it will be calculated automatically.

+5
source share

Yes, this is the constructor of std::vector . This one is:

 template <class InputIterator> vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() ); 

It takes two iterators (in the pointers of your case), from one to the beginning and the other to the end of the sequence of elements that should initialize the vector. The last parameter is optional, and you do not need it if you are not using custom allocators.

The iterator to the end should be one after the last element that you want to include. So, if you want all the elements from iarr[0] to iarr[9] , you need to go to iarr + 10 .

+3
source share

It highlights a vector using iterators and the original iarr []. There are ten elements, and +10 is the correct iteration, because this is one step at the end. The way vectors work - it should point to one position beyond the end. It copies the contents of the array to a vector. More clearly, this pattern is used to create a vector:

 template <class InputIterator> vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() ); 

Iteration constructor: Iterations between the first and last, setting a copy of each of the sequences of elements as the contents of the container.

+1
source share

This code is really 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 follows the usual convention [first, last [, so iarr + 10 is true if you want to copy the whole array

+1
source share

Yes, it is allowed, and the ivector will contain 10 elements. And no, this should not be 9 , because the final iterator should be one step below the end. If you know which ranges, this range will be represented by this range: [beginning, end) . That is, turn on the first, and go straight, but do not turn on the last.

Since STL (C ++ standard cough library) are all templates, everything that supports the ++ and * operators (dereference operator) can be passed as iterators to the vector constructor. This property surprisingly does the same code for both pointers and vector iterators. Standard library design at best.

0
source share

Yes, the last line in the main () function calls the constructor std :: vector. Look here to see all the overloads of the vector constructor. The third is used here. Its parameters are iterators for the template type (the template argument used here is int , and therefore the iterator is of type int* ). The second argument is an iterator that points to the first element of the sequence that will not be copied to the vector.

0
source share

All Articles