c ++在已知位置插入向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2330551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ insert into vector at known position
提问by myx
I wish to insert into a c++ vector at a known position. I know the c++ library has an insert() function that takes a position and the object to insert but the position type is an iterator. I wish to insert into the vector like I would insert into an array, using a specific index.
我希望在已知位置插入 c++ 向量。我知道 c++ 库有一个 insert() 函数,它需要一个位置和要插入的对象,但位置类型是一个迭代器。我希望像插入数组一样插入向量,使用特定的索引。
采纳答案by Scott Wolchok
Look at that debugging trace. The last thing that's executed is std::copy(__first=0x90c6fa8, __last=0x90c63bc, __result=0x90c6878). Looking back at what caused it, you called insert giving the position to insert at as 0x90c63bc. std::copy copies the range [first, last) to result, which must have room for last - first elements. This call has last < first, which is illegal (!), so I'm guessing that the position you're giving to insert at is wrong. Are you sure vnum hasn't underflowed somewhere along the line? In GDB with that trace showing, you should run
看看那个调试跟踪。最后执行的是 std::copy(__first=0x90c6fa8, __last=0x90c63bc, __result=0x90c6878)。回顾导致它的原因,您调用了插入,给出了插入位置为 0x90c63bc。std::copy 将范围 [first, last) 复制到结果,结果必须为 last - first 元素留出空间。这个调用有 last < first,这是非法的(!),所以我猜你要插入的位置是错误的。你确定 vnum 没有沿线某处下溢吗?在显示该跟踪的 GDB 中,您应该运行
frame 10
print vnum
第 10 帧
打印 vnum
to check. In fact, if you haven't just abbreviated in your question, I've just found your bug. Your second line is:
去检查。事实上,如果你没有在你的问题中缩写,我刚刚发现了你的错误。你的第二行是:
new_mesh->Face(face_loc)->vertices.insert(vertices.begin()+vnum+1, new_vertices[j]);
It should have been:
本来应该是:
new_mesh->Face(face_loc)->vertices.insert(new_mesg->Face(face_loc)->vertices.begin()+vnum+1, new_vertices[j]);
The first line gives the insertion point relative to the start of some othervariable called vertices, not the one you want to insert into.
第一行给出了相对于其他一些称为顶点的变量的起点的插入点,而不是您想要插入的那个。
回答by nevets1219
This should do what you want.
这应该做你想做的。
vector<int>myVec(3);
myVec.insert(myVec.begin() + INTEGER_OFFSET, DATA);
Please be aware that iterators may get invalidated when vector get reallocated. Please see this site.
请注意,当向量被重新分配时,迭代器可能会失效。请看这个网站。
EDIT: I'm not sure why the other answer disappeared...but another person mentioned something along the lines of:
编辑:我不知道为什么另一个答案消失了......但另一个人提到了一些类似的东西:
myVec.insert(INDEX, DATA);
myVec.insert(INDEX, DATA);
If I remember correctly, this should be just fine.
如果我没记错的话,这应该没问题。
回答by GManNickG
It's always nice to wrap these things up:
把这些东西包装起来总是很好的:
template <typename T>
T& insert_at(T& pContainer, size_t pIndex, const T::value_type& pValue)
{
pContainer.insert(pContainer.begin() + pIndex, pValue);
return pContainer;
}
That should do it. There is a now deleted answer that you can construct an iterator from an index, but I've never see that before. If that's true, that's definitely the way to go; I'm looking for it now.
那应该这样做。现在有一个已删除的答案,您可以从索引构造迭代器,但我以前从未见过。如果这是真的,那绝对是要走的路;我现在正在寻找。