Question discussion

What is the output of the following C++ program?#include #include using namespace std; int main() { vector v = {1, 2, 3}; v.push_back(4); v…

1520Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C++ program?

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> v = {1, 2, 3};
    v.push_back(4);
    v.erase(v.begin() + 1);
    cout << v.size() << v[1];
}
  • A43
  • B34
  • C23
  • D33
Correct answer: D

Explanation

After push_back the vector is {1, 2, 3, 4}; erasing index 1 leaves {1, 3, 4}, so the size is 3 and v[1] is 3.

Discussion 0

Nobody has replied yet. Start the discussion!