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.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Oracle equivalent of PostgreSQL INSERT…RETURNING *
- Converting glm::lookat matrix to quaternion and ba
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
to check. In fact, if you haven't just abbreviated in your question, I've just found your bug. Your second line is:
It should have been:
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.
This should do what you want.
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:If I remember correctly, this should be just fine.
It's always nice to wrap these things up:
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.