1

How to access the values stored in the data structure multiset, C++?

for (int i = 0; i < mlt.size; i++)
{
cout << mlt[i];
}
Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138

4 Answers4

5

If T is the type contained in your multiset,

for (std::multiset<T>::const_iterator i(mlt.begin()), end(mlt.end());
     i != end;
     ++i)
    std::cout << *i << "\n";
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
4

Look at this example: http://www.cplusplus.com/reference/stl/multiset/begin/

Basically, you can iterate through the multiset the same way as through any other stl container.

Vlad
  • 35,022
  • 6
  • 77
  • 199
3

You should not (normally) do so by writing a loop. You should normally use a pre-written algorithm, such as std::copy:

std::copy(mlt.begin(), mlt.end(), 
          std::ostream_iterator<T>(std::cout, "\n"));

Depending on the situation, there are quite a few variations that can be useful, such as using the infix_ostream_iterator I posted in a previous answer. This is useful primarily when you want to separate items in a list, to get (for example) 1,2,3,4,5 rather than the 1,2,3,4,5, that an ostream_iterator would produce.

Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 2
    I personally doubt that using copy `algorithm` for implementing quite obvious things like printing all the values makes the code clearer. Especially the example with infix operator proves my point. Just compare the amount of code needed for outputting `1, 2, 3, 4, 5` and the needed level of language mastering with for-loop and your approach. IMHO your approach introduces several unnecessary concepts (ostream_operator, copy), which don't make advocating advanced C++ features easy. – Vlad Jul 17 '11 at 15:35
  • Actually, +1. I at first thought the question was about `multimap`, then updated the answer to cover `multiset`, without switching to `copy` and `ostream_iterator`. I do agree with @Vlad, though, that your infix iterator is only useful in the presence of a C++ master who writes and maintains the code for it :) – Fred Foo Jul 17 '11 at 16:11
  • @Vlad: I can't agree. In fact, I'd say the *entirety* of the code using `copy` is easier to read, write, and understand than the code to define a `std::multiset::const_iterator` all by itself. Maintenance is also simpler -- consider what happens when the user decides to try out an unordered_multiset instead. With copy, no change is needed. With a loop, you need to change the most complex part of *every* instance. – Jerry Coffin Jul 17 '11 at 18:09
  • @larsmans: I'm not sure what maintenance would be needed -- I don't recall having made a single change to it since it was written. I wouldn't expect to maintain it any more than I'd expect to maintain `std::ostream_iterator`. – Jerry Coffin Jul 17 '11 at 18:22
  • @Jerry: imagine the case when you want to output it in form "1, 2, 3, 4, and 5". Will an average developer be able to modify the `infix_ostream_operator` accordingly? And won't it be trivial with for loop? Another concern: I personally don't associate printing the values with copying to a specially-crafted stream iterator, so the for loop reflects my way of thinking directly, in contrast to the copy/infix_ostream_operator. I admit that the other developers' way of thinking is different. – Vlad Jul 17 '11 at 21:42
  • @Vlad: It's certainly true that requirements could change in ways that suit one better than the other -- but at least IME, changes that would favor `std::copy` are *much* more common than those that would favor a `for` loop. – Jerry Coffin Jul 18 '11 at 05:29
1

auto for C++11 is convenience.

   for(auto t : mlt){
        cout << t << endl;
    }
fangzhzh
  • 2,172
  • 21
  • 25