[英]Vector size increases only by one when insert with multiple elements
I'm currently learning C++ and for my current goal I want to fill values from vector A
into vector X
.我目前正在学习 C++ ,对于我目前的目标,我想将
vector A
中的值填充到vector X
中。 vector X
must not be larger than 20. To archive this I have a function that checks if the current vector has space left. vector X
不能大于 20。为了存档,我有一个 function 来检查当前向量是否有剩余空间。 If no space is left, it replaces the current vector with a new vector from a template (which already contains some entries, that have to be the same for all the vectors):如果没有剩余空间,它将用模板中的新向量替换当前向量(该模板已经包含一些条目,所有向量必须相同):
int ChunkedBufferBuilder::checkNewBuffer() {
int space_left = chunk_size - super::current.size();
if (space_left == 0) {
bufVec.push_back(super::current);
super::current = tpl;
space_left = chunk_size - super::current.size() ;
}
return space_left;
}
bufVec
is another vector holding all vectors that have reached a size of 20. This function returns always the remaining amount of "free" entries in the current Vector. bufVec
是另一个向量,包含所有大小为 20 的向量。此 function 始终返回当前向量中剩余的“免费”条目数量。
To prevent having to insert every single value from the input vector in to the smaller vectors, I'm trying to use the insert
function here:为了避免必须将输入向量中的每个值插入到较小的向量中,我尝试在此处使用
insert
function:
ChunkedBufferBuilder* ChunkedBufferBuilder::push_back(const std::vector<uint8_t> &vec) {
auto pos = 0;
const auto size = vec.size();
while (pos < size) {
const int space_left = checkNewBuffer();
const int to = (space_left > size - pos) ? size : pos + space_left;
auto fromPtr = vec.at(pos);
auto toPtr = vec.at(to);
int size = super::current.size();
super::current.insert(super::current.end(), fromPtr, toPtr);
size = super::current.size();
pos = to;
}
return this;
}
Note, that the second size = super::current.size()
was placed there by me for helping to debug.请注意,我将第二个
size = super::current.size()
放在那里以帮助调试。 In the following images I have set two breakpoints.在下图中,我设置了两个断点。 One on the line where
insert
is called and the other one on the pos = to
assignment.一个在调用
insert
的行上,另一个在pos = to
assignment 上。
The documentation of std::vector
states that: std::vector
的文档指出:
The vector is extended by inserting new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.
通过在指定 position 的元素之前插入新元素来扩展向量,通过插入的元素数量有效地增加容器大小。
Thus I expect that the size increases by the amount of elements, that I added.因此,我希望尺寸会随着我添加的元素数量而增加。 But when I run the debugger:
但是当我运行调试器时:
I get these values at the first breakpoint:我在第一个断点处得到这些值:
And at the second breakpoint size
only increased by one:而在第二个断点
size
只增加了一个:
However on the second pass of the while loop, it then tries to insert another twelve values ( size
is still 8 so 20 - 8 == 12
):然而,在 while 循环的第二遍,它会尝试插入另外 12 个值(
size
仍然是 8 所以20 - 8 == 12
):
And then suddenly the size jumps to 22:然后突然大小跳到22:
This is currently breaking my program and I'm pretty much clueless why my code behaves in the way it currently does.这目前正在破坏我的程序,我几乎不知道为什么我的代码会以目前的方式运行。
In this snippet:在这个片段中:
auto fromPtr = vec.at(pos);
auto toPtr = vec.at(to);
super::current.insert(super::current.end(), fromPtr, toPtr);
you are inserting fromPtr
copies of the value toPtr
(ie overload (3)).您正在插入值
toPtr
的fromPtr
副本(即重载 (3))。 From your description, it appears you mean to use overload (4):根据您的描述,您的意思似乎是使用重载(4):
super::current.insert(super::current.end(), vec + pos, vec + to);
which copies elements from vec
to super::current
.它将元素从
vec
复制到super::current
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.