I would love to know what the code below means
I just want to know how it works tbh.
vector<int> lotteryNumVect(10); // I do not understand this part.
int lotteryNumArray[5] = {4, 13, 14, 24, 34}; // I understand this part.
lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray,
lotteryNumArray + 3); // I do not understand this part.
cout << lotteryNumVect.at(2) << endl; // I understand this part.
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
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 of lotteryNumVect
(see vector::begin()). Whereas lotteryNumArray
and lotteryNumArray+3
respectively points the first and the third elements of the lotteryNumArray
array.
So, basically lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray, lotteryNumArray + 3);
inserts the first three elements of the lotteryNumArray
to the beginning of the vector lotteryNumVect
.
cplusplus.com/reference/<type header name here>
cplusplus.com/reference/iostream/
cplusplus.com/reference/<the header which contains it>/<function/container/keyword name>
cplusplus.com/reference/iostream/cin/
cplusplus.com/reference/<the header which contains it>/<function/container/keyword name>/<member variable/function name>/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With