开发者

How to print values in multiset?

开发者 https://www.devze.com 2023-03-20 17:37 出处:网络
How to access the valu开发者_如何学编程es stored in the data structure multiset, C++? for (int i = 0; i < mlt.size; i++)

How to access the valu开发者_如何学编程es stored in the data structure multiset, C++?

for (int i = 0; i < mlt.size; i++)
{
cout << mlt[i];
}


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";


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.


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.


auto for C++11 is convenience.

   for(auto t : mlt){
        cout << t << endl;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号