c ++在已知位置插入载体 [英] c++ insert into vector at known position

查看:164
本文介绍了c ++在已知位置插入载体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在已知位置插入到c ++向量中。我知道c ++库有一个insert()函数,它接受一个位置和对象插入,但位置类型是一个迭代器。我想插入到向量中,就像使用一个特定的索引插入数组。

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.

推荐答案

。最后执行的是std :: copy(__ first = 0x90c6fa8,__last = 0x90c63bc,__result = 0x90c6878)。回想一下是什么原因,你调用insert给位置插入为0x90c63bc。 std :: copy将范围[first,last)复制到result,它必须有最后一个元素的空间。这个呼叫最后<第一,这是非法的(!),所以我猜你的位置你给插入是错误的。你确定vnum没有下溢沿线的某处?在GDB中显示该跟踪,您应该运行

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


框架10

frame 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]);

应该是:

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 other variable called vertices, not the one you want to insert into.

这篇关于c ++在已知位置插入载体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆