将 char 缓冲区附加到矢量;在STL中
将 C 缓冲区 (char *
) 的内容附加到 std::vector
末尾的正确(且有效)方法是什么?
What is the correct (and efficient) way of attaching the contents of C buffer (char *
) to the end of std::vector<char>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您有可用的
vector
时,您可能最好调用vector::insert
方法:最好将其委托给向量使用
back_inserter
因为向量可以决定其最终大小。back_inserter
只会push_back
,可能会导致更多的重新分配。When you have a
vector<char>
available, you're probably best calling thevector<char>::insert
method:Delegating it to the vector is to be preferred to using a
back_inserter
because the vector can then decide upon its final size. Theback_inserter
will onlypush_back
, possibly causing more reallocations.我认为正确的方法是
或
编辑:我重新排序了两个例子,所以这并不是评论者错了,这只是我的问题;-)
I think the proper way would be to
or
Edit: I reordered two examples, so it's not that commenters are wrong, it's just me ;-)
我没有编译过,但应该是这样的:
I haven't compiled it, but it should be something like: