将 char 缓冲区附加到 vector<char>在 STL [英] Attaching char buffer to vector&lt;char&gt; in STL

查看:33
本文介绍了将 char 缓冲区附加到 vector<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>?

推荐答案

当你有 vector 可用时,你可能最好调用 vector::插入方法:

When you have a vector<char> available, you're probably best calling the vector<char>::insert method:

std::vector<char> vec;

const char* values="values";
const char* end = values + strlen( values );

vec.insert( vec.end(), values, end );

将其委托给向量比使用 back_inserter 更可取,因为向量可以决定其最终大小.back_inserter 只会push_back,可能会导致更多的重新分配.

Delegating it to the vector is to be preferred to using a back_inserter because the vector can then decide upon its final size. The back_inserter will only push_back, possibly causing more reallocations.

这篇关于将 char 缓冲区附加到 vector<char>在 STL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆