方便地复制 std::vector输入流 (std::istream) 对象
我正在尝试使用第三方库中的一个函数,并且需要一个在其中传输二进制文件数据的输入流对象。
签名看起来像这样:
doSomething(const std::string& ...,
const std::string& ...,
std::istream& aData,
const std::string& ...,
const std::map<std::string, std::string>* ...,
long ...,
bool ...);
由于我无法更改/更改此第 3 方库/函数,因此我必须适应“我的”代码。在调用位置,我有一个 std::vector,其中包含预期在 istream 对象中传递的数据。目前,我通过迭代向量并使用 << 来将向量复制到流中。运算符逐字节复制。
我强烈怀疑可能有一种更有效/更方便的方法,但到目前为止还没有找到任何有用的方法。非常感谢任何帮助/您的想法。
最好的, JR
I'm trying to make use of a function which comes in a 3rd party lib and expects an input stream object in which binary file data is transported.
Signature looks like that:
doSomething(const std::string& ...,
const std::string& ...,
std::istream& aData,
const std::string& ...,
const std::map<std::string, std::string>* ...,
long ...,
bool ...);
Since I can't alter/change this 3rd party lib/function, I have to adapt in "my" code. In the calling place, I have a std::vector which contains the data which is expected to be passed in an istream object. Currently I copy the vector to the stream, by iterating over it and using the << operator to copy byte by byte.
I strongly suspect that there might be a more effective/convenient way but couldn't find anything useful up to now. Any help/your thoughts are greatly appreciated.
Best,
JR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用字符向量作为输入流的基础缓冲区,而无需复制向量的内容:
@NeilKirk 报告 上述使用
pubsetbuf
的方法是不可移植的。一种可移植的方法是使用boost::iostreams库。这是如何从向量构造输入流而不复制其内容:
You can use a
vector
of characters as an underlying buffer for an input stream without copying the contents of the vector:@NeilKirk reports that the above method of using
pubsetbuf
is non-portable.One portable way is to use
boost::iostreams
library. This is how to construct an input stream from a vector without copying its contents: