简体   繁体   English

打印boost :: array <>到标准输出

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM