6

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.
8
  • We love that you're here and this is not a working example.
    – nicomp
    Aug 6, 2019 at 17:51
  • vector <int> lotteryNumVect(10); creates a vector of int with 10 default constructed ints in it.
    – Ted Lyngmo
    Aug 6, 2019 at 17:53
  • @nicomp what do you mean?
    – Zav
    Aug 6, 2019 at 17:53
  • Your code was filled with * characters that made it invalid but it's better now.
    – Ted Lyngmo
    Aug 6, 2019 at 17:54
  • Thanks @Ted Lyngmo
    – Zav
    Aug 6, 2019 at 17:54

3 Answers 3

7

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 
7
  • thank you I liked your post but it wont publicly display it since i have a reputation lower than 15.
    – Zav
    Aug 6, 2019 at 17:58
  • 1
    @Zav You are still able to accept it as an answer.
    – Ted Lyngmo
    Aug 6, 2019 at 17:59
  • @Zav No problem.:) Aug 6, 2019 at 17:59
  • 1
    If you deem it as the best answer click on the check icon. It will mark the answer as accepted. Aug 6, 2019 at 17:59
  • 1
    My first time using stackoverflow Im just a 12 year old in my bedroom lol
    – Zav
    Aug 6, 2019 at 18:08
5

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

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.

3
  • Thanks for the help Meraj al Maksud really appreciate it. I would put a check on your answer ,but it was not simple enough. But, your answer is definitely the best in-depth answer but is a bit confusing nontheless, good job :)
    – Zav
    Aug 7, 2019 at 0:08
  • No @zav, my answer is basically the same. I just added some more info for further reading. I'm hopeful that it will come handy for you in future as it did for me. Aug 7, 2019 at 0:25
  • ok @Meraj al Maksud :)
    – Zav
    Aug 7, 2019 at 17:49
4

Let's go through step by step

vector<int> lotteryNumVect(10);
  • Create a vector of ints. 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 in lotteryNumVect
3
  • Create a vector of ints. Set the size to 10.. that's inaccurate, what is the value of the int?
    – t.niese
    Aug 6, 2019 at 17:57
  • 1
    It will create a vector of 10 ints, all set to 0 Aug 6, 2019 at 17:57
  • thanks for trying to help.
    – Zav
    Aug 6, 2019 at 18:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.