[英]Printing boost::array<> to stdout
I have a boost::array
declared as below 我有一个
boost::array
声明如下
typedef boost::array<unsigned char, 4096> m_array_type;
m_array_type m_recv_buf;
I want to output m_recv_buf
to stdout
. 我想将
m_recv_buf
输出到stdout
。
std::cout << m_recv_buf << std::endl;
doesn't seem to work? 似乎不起作用? How do I print this to
stdout
如何将其打印到
stdout
Use ostream.write : 使用ostream.write :
std::cout.write(&m_recv_buf[0], len);
Also, you specified the C++11 tag. 另外,您指定了C ++ 11标记。 Consider using std::array, which is now standard.
考虑使用std :: array,它现在是标准的。
If you want to print your m_array_type
as a string, you'll need the string to properly end with a null terminator. 如果要将
m_array_type
打印为字符串,则需要该字符串正确以空终止符结尾。 So this approach creates a std::string
type, explicitly sending the length to the std::string
constructor. 因此,此方法将创建一个
std::string
类型,将长度明确发送给std::string
构造函数。
std::cout << std::string(reinterpret_cast<const char*>(m_recv_buf.data()), m_recv_buf.size()) << std::endl;
I think this way would be clear to others who have to use your code. 我认为这种方式对其他必须使用您的代码的人来说很清楚。 But the previous answer would be way better if
m_array_type
ever changes it's type. 但是,如果
m_array_type
更改其类型,则先前的答案会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.