简体   繁体   English

插入多个元素时,向量大小仅增加一

[英]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)).您正在插入toPtrfromPtr副本(即重载 (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.

相关问题 将多个偏移量的多个元素插入向量中 - Insert multiple elements at multiple offsets into an vector 向量的一个索引中的多个元素 - Multiple Elements in one index of vector 为什么当我将其大小初始化为 5 并且只给出 5 个元素时,向量的大小会增加到 10? - Why is the vector increasing its size to 10 when i am initialising its size to 5 and giving only 5 elements? 使用std :: vector :: push_back插入未知数量的元素时,是否应在每次推送时检查std :: vector :: max_size? - When using std::vector::push_back to insert an unknown number of elements, should std::vector::max_size be checked on each push? 当已知大小时,对向量添加元素进行基准测试 - Benchmarking adding elements to vector when size is known 如何在多个线程中仅插入向量一次 - how to insert vector only once in multiple thread 如何使用迭代器在向量中的不同位置插入多个元素? - How to insert multiple elements in the vector at different positions using iterator? 大小增加时在编译数组数组时崩溃 - Crash on compiling an array of arrays when size increases 如何仅在模板 class 中的一种向量中插入数据? - How to insert data in only one type of Vector in template class? 当向量中唯一元素的数量远小于向量大小时,有效地处理向量的每个唯一排列 - Efficiently process each unique permutation of a vector when number of unique elements in vector is much smaller than vector size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM