-6

I have implemented a multi-map, but I​ was wondering how do I access the first 5 elements of a multimap?

I tried using a for-loop, but that didn't work out. Any suggestions?

  • 3
    What do you mean? Are you talking about a `std::multimap`? If not, we don't know the interface of your own implementation, without code. – Superlokkus Jun 14 '19 at 23:08
  • 5
    Please post the code you tried (in the form of a [MCVE]). – Paul Sanders Jun 14 '19 at 23:17
  • 3
    You probably didn't implement a multimap but you may using one. Where's the code? – Bayleef Jun 14 '19 at 23:36
  • 1
    How can you think that posting a screenshot of your code could be any better than pasting the code directly? – Jack Jun 15 '19 at 00:37
  • 2
    Please post code as text, not an image. See the markdown formatting help page if you need help formatting it as a code block. Also, please make a [mre]. – eesiraed Jun 15 '19 at 00:39
  • The included code sample is much better than the image link you had before. Best of all would be to make it a short, complete source file that we can compile without any additional modifications. – Davislor Jun 17 '19 at 01:54

1 Answers1

1

Although it would be easier to help you if you were to post a minimal reproducible example, as text, formatted as a code sample, I think I still understand what you’re asking.

This looks like a learning exercise that you want to solve on your own. But I can give some advice.

What you want to do is check two conditions: that you’ve read five elements, or that you’ve run out of them. Declare both a loop counter initialized to 0 and an iterator.initialized to .begin(). Loop until the counter is equal to 5 or the iterator is equal to .end(). On each iteration, increment both the counter and the iterator. You might express this as a while loop, but you could also do it with comma operators in your for loop.

Also, please indent your code properly and use braces under your for and if statements. It’ll save you from writing a lot of bugs and make your code much easier to read.

Davislor
  • 14,674
  • 2
  • 34
  • 49