Why does the 'size' have a value of 4? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does the 'size' have a value of 4?

int size = 0; vector <int> my vector (4,4); // elements in the vector vector <int> :: iterator it; it = myvec.begin (); // .begin() is the 1st element in the vector for (it; it!=myvec.end(); it++){ // .end() is the past-the-end element (theoretical) size++; } int value = myvec.back(); // .back() is the last element in the vector cout << size *value; // the output is 16 Questions: 1) What does line 2's 'iterator' do? 2) What is the meaning of it++ in line 5? 3) Why does the 'size' variable, in the last line of code, have a value of 4?

31st Jul 2020, 9:25 AM
Solus
Solus - avatar
9 Answers
+ 5
https://code.sololearn.com/cu6qqzPJ6VRn/?ref=app vector<int> myvec(size, value) is the syntax.
31st Jul 2020, 9:56 AM
Rohit
+ 2
What is iterator? An iterator is an object (like a pointer) that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualized as something similar to a pointer pointing to some location and we can access the content at that particular location using them. (Defn Source: geeksforgeeks) (In simple words, iterators are used for traversing the elements of the container like vector, map etc.) I've provided the code, you can check what's that line- vector<int> myvec(4,4) doing? It's basically filling the vector of size 4 with value 4's. (or fill the myvec with 4 four times). 2) What is the meaning of it++ in line 5? => You need to iterate/traverse each element of the vector that's why you need to increment the iterator so to access each elements in myvec. 3) Why does the 'size' variable, in the last line of code, have a value of 4? => So it's because we have make myvec to be of that size by using this vector<int> myvec(4,4).
31st Jul 2020, 9:55 AM
Rohit
+ 2
Solus In vector <int> variable(size, value) the size is already given so we don't need to reassign any to it and ofcourse we don't need to use the for loop for finding the size. for printing the size we can simply do this - cout << variable. size();
31st Jul 2020, 7:46 PM
Rohit
+ 2
Solus Value of <it> is the address of an element in a container, as RKK had explained early on, iterator is a special pointer used to traverse a container. To know the actual element's value we need to dereference the iterator. Is it 1 or 4? Depends, where you point the iterator. It could point to the first element (<container>.begin()) or others. Again, as RKK explained, the loop is unnecessary. I just noticed that you wrote the loop to count the container's elements. In that case of course we can instead use `size()` function. My previous note on the loop wss referring to the for-loop initialization for(it; it != myvec.end(); it++) ☝ I was saying that <it> doesn't need to be in the initialization block, it has no effect as <it> had been defined before the loop.
31st Jul 2020, 8:33 PM
Ipang
+ 1
Ipang What exactly is the value of 'it'? Is it 1 (denoting first element)? Or is it 4 (denoted by the second 4 in vector <int> my vector (4,4))? ~~~~~~~^ Is the for loop even necessary if vector<int> variable(size, value) in line 2 has already specified the size? Seems redundant... From my understanding, the size is no longer 0 by the time we get to line 5. Is this incorrect? Or does the size from vector<int> variable(size, value) differ from the VARIABLE size? --> in which would make the for loop useful...
31st Jul 2020, 7:42 PM
Solus
Solus - avatar
+ 1
Also why make things harder/complex when syntax are already being designed for it. I'm talking about these lines - vector<int>::iterator it; it = myvec.begin(); for (it=myvec.begin(); it!=myvec.end(); it++){ size++; } the same thing can be achieve using - for (auto it=myvec.begin(); it!=myvec.end(); it++){ size++; } this is just an example and we don't even need the above for loop infact, it's just redundant we can know the size of a vector by using - vector_name.size()
31st Jul 2020, 7:54 PM
Rohit
0
Solus The <it> inside for-loop initialization has no effect (gets a warning). <it> had already been defined outside the loop, so no need for <it> in the loop definition. for (; it!=myvec.end(); it++) // no <it> in initialization
31st Jul 2020, 10:19 AM
Ipang
0
RKK According to https://www.bitdegree.org/learn/c-plus-plus-vector, the syntax is vector <type> variable (elements). Are you saying that vector<int> variable(size, value) is equivalent? (ie. that whatever is in the parenthesis is set to be the elements?) Does vector<int> variable(size, value) re-assign the size variable to size =4? Or is this 4 value completely separate from the size variable in line 1? Confusion: Is the for loop even necessary if vector<int> variable(size, value) has already specified the size? Seems redundant...
31st Jul 2020, 7:34 PM
Solus
Solus - avatar
0
just now that the size of this vector <int> variable(size, value) would be fixed. since we are initialize it. However if you want vector that is not fixed in size use this version - vector<int> variable;
31st Jul 2020, 7:49 PM
Rohit