如何在多集中打印值
How to print values in multiset?
如何访问存储在数据结构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;
}
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 如何从存储在std::映射中的std::集中删除元素
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 我想为每个组合框选择分配整数值,并将它们集中打印到文本文件中
- 是否有任何库可以确定数值是否可以转换为特定字符集中的有效、可打印且有意义的字符
- 如何在多集中打印值