简体   繁体   English

如何将二进制数据复制到字符串流

[英]How do I copy binary data to a stringstream

I have a std::vector<int> and I want serialize it. 我有一个std::vector<int> ,我想序列化它。 For this purpose I am trying to use a std::stringstream 为此我试图使用std::stringstream

 vector<int> v;
 v.resize(10);
 for (int i=0;i<10;i++)
 v[i]=i;


 stringstream ss (stringstream::in | stringstream::out |stringstream::binary);

However when I copy the vector to the stringstream this copy it as character 但是,当我将矢量复制到stringstream时,将其复制为字符

ostream_iterator<int> it(ss);
copy(v.begin(),v.end(),it);

the value that inserted to buffer(_Strbuf) is "123456789" 插入缓冲区(_Strbuf)的值是“123456789”

I sucssesed to write a workaround solution 我成功地编写了一个解决方案

for (int i=1;i<10;i++)
   ss.write((char*)&p[i],sizeof(int));

I want to do it something like first way by using std function like copy 我想通过使用像复制这样的std函数来做类似第一种方式的事情

thanks Herzl 谢谢Herzl

Actually, this is your workaround but it may be used with std::copy() algorithm. 实际上,这是你的解决方法,但它可以与std :: copy()算法一起使用。

   template<class T>
   struct serialize
   {
      serialize(const T & i_value) : value(i_value) {}
      T value;
   };

   template<class T>
   ostream& operator <<(ostream &os, const serialize<T> & obj)
   {
      os.write((char*)&obj.value,sizeof(T));
      return os;
   }

Usage 用法

ostream_iterator<serialize<int> > it(ss);
copy(v.begin(),v.end(),it);

I know this is not an answer to your problem, but if you are not limited to the STL you could try ( boost serialization ) or google protocol buffers 我知道这不是你的问题的答案,但如果你不限于STL你可以尝试( 提升序列化 )或谷歌协议缓冲

Boost even has build-in support for de-/serializing STL containers (http://www.boost.org/doc/libs/1_45_0/libs/serialization/doc/tutorial.html#stl) 甚至还提供了对STL容器进行de / /序列化的内置支持(http://www.boost.org/doc/libs/1_45_0/libs/serialization/doc/tutorial.html#stl)

To use std::copy with ostream::write, you'd need to write your own output iterator that knows how to correctly serialize the type. 要将std :: copy与ostream :: write一起使用,您需要编写自己的输出迭代器,它知道如何正确地序列化类型。 That said, I'm not sure what you expect to gain from this approach, but here's a first pass at that for an example: 也就是说,我不确定你希望从这种方法中获得什么,但这里的第一步是一个例子:

struct ostream_write_int
  : std::iterator<std::output_iterator_tag, int, void, void, void>
{
  std::ostream *s;
  ostream_write_int(std::ostream &s) : s (&s) {}

  ostream_write_int& operator++() { return *this; }
  ostream_write_int& operator++(int) { return *this; }
  ostream_write_int& operator*() { return *this; }

  void operator=(int x) {
    s->write(reinterpret_cast<char*>(&x), sizeof(x));
  }
};

This could be templated only if you defer the serialization logic to some other function (as the formatted stream iterators do to operator<<). 只有将序列化逻辑推迟到某个其他函数(如格式化的流迭代器对operator <<)那样,才可能模板化。

像弗雷德一样,我没有看到这一点,你有效地尝试做的是:

ss.rdbuf()->sputn(reinterpret_cast<char*>(&v[0]), sizeof(int) * v.size());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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