Confusion about vectors
This statement
vector <int> lotteryNumVect(10);
declares a vector with 10 elements initialized by zeroes.
That is there is used the constructor
explicit vector(size_type n, const Allocator& = Allocator());
3 Effects: Constructs a vector with n default-inserted elements using the specified allocator.
The second parameter of the constructor have a default argument so you may call the constructor specifying only the number of elements to be created in a vector.
This statements
lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray,
lotteryNumArray + 3);
inserts in the beginning of the vector 3 elements from the array.
So as a result the vector will look like
4, 13, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Explanation
Statement
vector <int> lotteryNumVect(10);
:This is an example of using constructor. According to cplusplus:
default (1) :
explicit vector (const allocator_type& alloc = allocator_type());
fill (2) :
explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());
range (3) :
template <class InputIterator> vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
copy (4) :
vector (const vector& x);
So,
vector <int> lotteryNumVect(10);
initializes the vector with ten zeros (see (1) above).vector <int> lotteryNumVect(5, 2);
would initialize the vector with five twos (see (2) above). You can check the example here to understand better.Statement
lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray, lotteryNumArray + 3);
:This actually insertion via iterators. Check this out:
single element (1) :
iterator insert (iterator position, const value_type& val);
fill (2) :
void insert (iterator position, size_type n, const value_type& val);
range (3) :
template <class InputIterator> void insert (iterator position, InputIterator first, InputIterator last);
The term
lotteryNumVect.begin()
actually points the first element oflotteryNumVect
(see vector::begin()). WhereaslotteryNumArray
andlotteryNumArray+3
respectively points the first and the third elements of thelotteryNumArray
array. So, basicallylotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray, lotteryNumArray + 3);
inserts the first three elements of thelotteryNumArray
to the beginning of the vectorlotteryNumVect
.
Further reading on std::vector
- cpluscplus
- cppreference
- GeeksforGeeks
How to navigate on cplusplus:
- Header:
cplusplus.com/reference/<type header name here>
Example:cplusplus.com/reference/iostream/
- Function/Container/Keyword:
cplusplus.com/reference/<the header which contains it>/<function/container/keyword name>
Example:cplusplus.com/reference/iostream/cin/
- Member function/variable:
cplusplus.com/reference/<the header which contains it>/<function/container/keyword name>/<member variable/function name>/
Example:cplusplus.com/reference/string/string/size/
Alternatively, you could Google it. At which, you will get all of three sites in your search result and perhaps far better outcome.
Let's go through step by step
vector<int> lotteryNumVect(10);
- Create a vector of
int
s. Set the size to 10.
lotteryNumVect.insert(lotteryNumVect.begin(), // Place to insert stuff
lotteryNumArray, // Pointer to start of thing to insert
lotteryNumArray + 3); // Pointer to end of stuff to insert
- Insert the first 3 things in
lotteryNumArray
inlotteryNumVect