Question discussion

What is the output of the following C++ program?#include #include #include using namespace std; int main() { map m; m["a"]++; m["b"] += 2;…

2146Medium DSSSB TGT Computer Science 0 replies 1 views
0

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

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
    map<string, int> m;
    m["a"]++;
    m["b"] += 2;
    cout << m.size() << " ";
    cout << m["c"] << " ";
    cout << m.size();
    return 0;
}
  • A2 0 2
  • B2 1 3
  • C3 0 3
  • D2 0 3
Correct answer: D

Explanation

operator[] inserts a missing key with a value-initialised 0, so reading m["c"] gives 0 and raises the size from 2 to 3.

Discussion 0

Nobody has replied yet. Start the discussion!