I have the following question dealingwith the vector class in C++ and just want
ID: 3609219 • Letter: I
Question
I have the following question dealingwith the vector class in C++ and just want to be sure that Iunderstand it correctly. Give the value of v.size() after executingeach of the following statements:vector<int> v(25);
v.push_back(-4);
v.resize(40);
m = v.back();
v.pop_back();
If I understand this correctly, v.size() should be asfollows after each respective statement is executed:
1) v.size() = 1
2) v.size() = 2
3) v.size() = 39
4) v.size() = 39
5) v.size() = 38
If I am incorrect, please help with an explanation. Thanks inadvance! I have the following question dealingwith the vector class in C++ and just want to be sure that Iunderstand it correctly. Give the value of v.size() after executingeach of the following statements:
vector<int> v(25);
v.push_back(-4);
v.resize(40);
m = v.back();
v.pop_back();
If I understand this correctly, v.size() should be asfollows after each respective statement is executed:
1) v.size() = 1
2) v.size() = 2
3) v.size() = 39
4) v.size() = 39
5) v.size() = 38
If I am incorrect, please help with an explanation. Thanks inadvance!
Explanation / Answer
Here are the correct answers: 1: 25 2: 26 3: 40 4: 40 5: 39 So let's start at the top. vector v(25); This simply declares (creates) a vector of size 25. v.push_back(-4); This statement adds the value -4 onto the end of the vector. So nowthere will be one more space in the vector declared previously innumber 1, creating 25 +1 = 26. v.resize(40);This statement resizes the "v"vector, so that it now contains 40 spaces. m = v.back();This statement simply sets m tothe last value in the vector. It will not affect the size of thevector at all. So we are still at 40. v.pop_back(); Pop Back is like the opposite of Push Back. It will delete the lastelement from the vector. So now we have 40 - 1 = 39.Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.