填充 std::vector的最佳方式缓冲
该缓冲区应包含长度相等的槽(本例中为 3 个)(本例中为 20)
缓冲区必须具有连续的内存,以便可以以非常量方式传递给 C 函数。
const int slot_size = 20;
std::vector<char> vbuffer;
该函数接受一个字符串,复制到所需大小的临时缓冲区,然后将其附加到vbuffer
void prepBuffer( const std::string& s)
{
std::vector<char> temp(slot_size);
std::copy(s.c_str(), s.c_str() + s.length() + 1, temp.begin());
vbuffer.insert(vbuffer.end(), temp.begin(), temp.end());
}
测试函数
int main()
{
vbuffer.reserve(60);
prepBuffer( "Argentina");
prepBuffer( "Herzegovina");
prepBuffer( "Zambia");
cout << &vbuffer[0] << endl;
cout << &vbuffer[20] << endl;
cout << &vbuffer[40] << endl;
}
问题。有很多在我的 prepBuffer
函数中复制字符串。我正在寻找一种更好的方法来以最少的复制来填充 vbuffer
编辑
插槽的大小由程序中的其他部分确定。但在编译时并不知道。
编辑
根据下面我接受的答案,我已经选择了这个版本
void prepBuffer(const std::string& s)
{
assert(s.size() < slot_size );
vbuffer.insert(vbuffer.end(), s.begin(), s.end());
vbuffer.insert(vbuffer.end(), slot_size - s.size(), '\0' );
}
仍然欢迎提出建议
This buffer should contain slots (three in this example) of equal length ( 20 in this example)
The buffer has to have contiguous memory so that it can be passed to a C function in non-const fashion.
const int slot_size = 20;
std::vector<char> vbuffer;
This function takes a string, copies to a temporary buffer of the required size then appeds it to vbuffer
void prepBuffer( const std::string& s)
{
std::vector<char> temp(slot_size);
std::copy(s.c_str(), s.c_str() + s.length() + 1, temp.begin());
vbuffer.insert(vbuffer.end(), temp.begin(), temp.end());
}
Testing the function
int main()
{
vbuffer.reserve(60);
prepBuffer( "Argentina");
prepBuffer( "Herzegovina");
prepBuffer( "Zambia");
cout << &vbuffer[0] << endl;
cout << &vbuffer[20] << endl;
cout << &vbuffer[40] << endl;
}
Question. There is a lot of string copying in my prepBuffer
function. I am looking for a better way to fill up vbuffer
with minimal copying
EDIT
The size of slots is determined elsewhere in the program. But it is not known at compile time.
EDIT
In line with my accepted answer below, I have settled on this version
void prepBuffer(const std::string& s)
{
assert(s.size() < slot_size );
vbuffer.insert(vbuffer.end(), s.begin(), s.end());
vbuffer.insert(vbuffer.end(), slot_size - s.size(), '\0' );
}
Suggestions are still welcome
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
怎么样:
建议对字符串长度进行额外检查,并制定处理超长字符串的策略(例如
assert(s.size() < 20);
)。How about this:
An additional check on the string length is recommended, along with a policy for handling over-long strings (e.g.
assert(s.size() < 20);
).如果您根本不使用
std::string
并避免使用临时std::vector
,则无需任何额外的动态分配即可轻松完成此操作。或者,由于要写入的字符数提前已知,因此您可以轻松地使用非模板函数:
If you don't use
std::string
at all and avoid the temporarystd::vector
, you can easily do this without any extra dynamic allocation.Or, since the number of characters to be written is known ahead of time, you could just as easily use a nontemplate function:
另一个想法:
您可以这样写
如果您想在更改嵌套数组的大小时更加灵活,
Another idea:
You could write
in case you wanted to be a bit more flexible when changing the size of the nested array
功能
测试以可维护性为代价最大限度地减少复制的
。
Here is nearly-optimal code that is still readable and maintainable.
Testing the function
That minimizes copying, at the cost of maintainability.
Here is nearly-optimal code that is still readable and maintainable.