How to save `std::vector<uchar>` into `std::ostream`?

泪湿孤枕 提交于 2019-12-04 05:16:50

Use write:

void send_data(std::ostream & o, const std::vector<uchar> & v)
{
  o.write(reinterpret_cast<const char*>(v.data()), v.size());
}

The ostream expects naked chars, but it's fine to treat uchars as those by casting that pointer. On older compilers you may have to say &v[0] instead of v.data().

You can return the result of write() as another std::ostream& or as a bool if you like some error checking facilities.

Depending on how you want the output formatted, you might be able to use copy in conjunction with ostream_iterator:

#include <iterator>

/*...*/

copy( v.begin(), v.end(), ostream_iterator<uchar>(my_stream,"") );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!