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 CPP Exam - Topic 7 Question 128 Discussion

What will happen when you attempt to compile and run the following code?#include #include #include #include #include using namespace std;int main() {int t[] = { 3, 4, 2, 1, 0, 3, 4, 1, 2, 0 };vector v(t, t + 10);multimap m;for (vector::iterator i = v.begin(); i != v.end(); i++) {stringstream s;s
A) 2 2
B) 1 2
C) 1 3
D) 2
E) 0 2

C++ Institute CPP Exam - Topic 7 Question 128 Discussion

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

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

#include

#include

#include

#include

#include

using namespace std;

int main() {

int t[] = { 3, 4, 2, 1, 0, 3, 4, 1, 2, 0 };

vector v(t, t + 10);

multimap m;

for (vector::iterator i = v.begin(); i != v.end(); i++) {

stringstream s;s << *i << *i;

m.insert(pair(*i, s.str()));

}

pair::iterator, multimap::iterator> range;

range = m.equal_range(2);

for (multimap::iterator i = range.first; i != range.second; i++) {

cout << i?>first << " ";

}

return 0;

}

The output will be:

Show Suggested Answer Hide Answer
Suggested Answer: A

Contribute your Thoughts:

0/2000 characters
Edda
3 days ago
I feel like it should be option A too.
upvoted 0 times
...
Carmen
8 days ago
I think it will output 2 2.
upvoted 0 times
...
Dalene
13 days ago
The stringstream part is clever!
upvoted 0 times
...
Jerlene
18 days ago
I thought it would be "1 2" instead.
upvoted 0 times
...
Melinda
23 days ago
Wait, how does it get "2 2"?
upvoted 0 times
...
Katina
28 days ago
Definitely outputs "2 2".
upvoted 0 times
...
Adelaide
1 month ago
The code compiles without errors.
upvoted 0 times
...
Ernie
1 month ago
I believe the output should be just "2" since that's the only key that matches in the multimap, but I might be mixing it up with another question we did.
upvoted 0 times
...
Olive
1 month ago
I’m a bit confused about the `stringstream` part. Does it actually concatenate the number twice? I feel like that might affect what gets stored in the multimap.
upvoted 0 times
...
Louis
2 months ago
I remember something similar from practice where we used `equal_range` with a multimap. I think it should return all entries with the key 2, but I can't recall what those entries were.
upvoted 0 times
...
Katie
2 months ago
I think the code will compile fine, but I'm not sure about the output. It seems like it should print the values associated with the key 2.
upvoted 0 times
...

Save Cancel