Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about vectors [closed]

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.
like image 253
Zav Avatar asked Aug 06 '19 17:08

Zav


2 Answers

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 
like image 102
Vlad from Moscow Avatar answered Sep 23 '22 23:09

Vlad from Moscow


Explanation

  1. 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.

  2. 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.


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.

like image 29
Meraj al Maksud Avatar answered Sep 22 '22 23:09

Meraj al Maksud