简体   繁体   English

用于2D矢量的C ++插入命令不起作用

[英]C++ Insert command for 2D vector does not work

My program is very long so I would just simplify the problematic codes here: 我的程序很长,所以我只在这里简化有问题的代码:

class C{...};

vector<vector<C>> A;
vector<C> B;

int i = 0;
cin >> i;

A.insert(A.begin()+i, B); 

Here, when I change the last line to "A.push_back(B);" 在这里,当我将最后一行更改为“ A.push_back(B);”时 or "A.insert(A.begin()+0, B), the run-time error is gone, therefore I suspect the problem is with the 2d vector size and position. But I just want the vector B to be inserted into the 2d vector A at a specific position only! Please help :'( 或“ A.insert(A.begin()+ 0,B),运行时错误消失了,因此我怀疑问题出在二维矢量的大小和位置上。但是我只想将矢量B插入仅在特定位置的2D向量A!请帮助:'(

Your vector has zero size. 您的向量大小为零。

While vectors are 'dynamic' structures, they don't grow of their own accord. 尽管向量是“动态”结构,但它们并不会自动增长。 Accessing elements outside the current bounds will result in undefined behaviour. 访问超出当前范围的元素将导致不确定的行为。

If you want the vector to have at least i elements, you can do this before the insert. 如果希望向量至少包含i元素,则可以在插入之前执行此操作。

if (i < A.size())
  A.resize(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM