Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPP Topic 6 Question 105 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 105
Topic #: 6
[All CPP Questions]

What happens when you attempt to compile and run the following code?

#include

#include

#include

using namespace std;

void myfunction(pair i) {

cout << " " << i.first;

}

int main() {

int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };

map m;

for(int i=0; i < 10; i++) {

m[i]=t[i];

}

for_each(m.begin(), m.end(), myfunction);

return 0;

}

Program outputs:

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

Dulce
9 days ago
I'm not sure about that. I think there might be a compilation error because the myfunction is expecting a pair but the map contains pairs of int, int. So, the correct answer might be E) compilation error.
upvoted 0 times
...
Reiko
11 days ago
I agree with Cristy. The code is iterating over the map and calling the myfunction for each pair, which will output the keys in ascending order.
upvoted 0 times
...
Cristy
13 days ago
I think the output will be B) 0 1 2 3 4 5 6 7 8 9 because the code is using a map to store key-value pairs and then iterating over it using for_each.
upvoted 0 times
...

Save Cancel