views:

591

answers:

3

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.

+1  A: 

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.

GMan
is this equivalent to doing something like myVector.insert(myVector.begin()+index_num-1, object)?
myx
@myx: It's exactly that, without the minus one.
GMan
+2  A: 
nevets1219
Thanks for catching my mistake...my habit of setting things to null seems to have gotten the better of me. Also corrected mistake in code.
nevets1219
when I do this new_mesh->Face(face_loc)->vertices[vnum] = new_vertices[new_vertices.size()-1];new_mesh->Face(face_loc)->vertices.insert(vertices.begin()+vnum+1, new_vertices[j]), I get a seg fault after the insert. Here, j=0, vnum=0, vertices is std:vector type
myx
@myx: Is there a reason you're not using `std::vector`?
GMan
my c++ vector, I meant std::vector
myx
Shouldn't be a vector issue, as it should get reallocated if the capacity is exceeded. Note this invalidates previous iterators as noted in http://www.cplusplus.com/reference/stl/vector/insert/
nevets1219
new_vertices is declared as vector <R3MeshVertex *> new_vertices. Could it be an issue with pointers?
myx
As Joe pointed out, you CANNOT use an integer to indicate the position but you can still use an "iterator + offset" to do what you want. I'll edit so that there's a clearer example of that.
nevets1219
thanks - I got the offset part working and now I'm trying to solve the mysterius seg fault. Can you take a look at my edited initial post?
myx
Please refer to my comment in your question. I hope that's the issue.
nevets1219
A: 

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

frame 10

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

Scott Wolchok