如何访问存储在数据结构 multiset、C++ 中的值?
for (int i = 0; i < mlt.size; i++)
{
cout << mlt[i];
}
如何访问存储在数据结构 multiset、C++ 中的值?
for (int i = 0; i < mlt.size; i++)
{
cout << mlt[i];
}
如果T
是您的多重集中包含的类型,
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 容器一样遍历多重集。
您不应该(通常)通过编写循环来这样做。您通常应该使用预先编写的算法,例如std::copy
:
std::copy(mlt.begin(), mlt.end(),
std::ostream_iterator<T>(std::cout, "\n"));
视情况而定,有很多有用的变体,例如使用infix_ostream_iterator
我在上一个答案中发布的。这主要在您想要分隔列表中的项目时很有用,以获取(例如)1,2,3,4,5
而不是1,2,3,4,5,
anostream_iterator
将产生的。
C++11 的 auto 很方便。
for(auto t : mlt){
cout << t << endl;
}