如何在多集中打印值

How to print values in multiset?

本文关键字:打印 集中      更新时间:2023-10-16

如何访问存储在数据结构multiset中的值,c++ ?

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

如果T是multiset中包含的类型,

for (std::multiset<T>::const_iterator i(mlt.begin()), end(mlt.end());
     i != end;
     ++i)
    std::cout << *i << "n";

看这个例子:http://www.cplusplus.com/reference/stl/multiset/begin/

基本上,可以像遍历其他stl容器一样遍历multiset。

您应该而不是(通常)通过编写循环来这样做。通常应该使用预先编写的算法,例如std::copy:

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

根据情况,有相当多的变化,可以是有用的,如使用infix_ostream_iterator我张贴在以前的答案。当您想要分离列表中的项目时,这主要是有用的,例如获得(例如)1,2,3,4,5而不是ostream_iterator将产生的1,2,3,4,5,

auto用于c++ 11是方便的。

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