Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Bizarre vector insertion behavior - can someone please enlighten?

Thread Tools

Bizarre vector insertion behavior - can someone please enlighten?
Posts: n/a
I've run into a seemingly bizarre problem with insert() for
std::vector. (This was done on Microsoft Visual C++ 2005 express
version 8...maybe it is a compiler specific bug?)

Here's the code:


// vector tester 3.cpp : main project file.

#include "stdafx.h"

int main(array<System::String ^> ^args)
std::vector<int> vectorList;

for (unsigned int i = 0; i < 45; ++i) // works if 45 is
replaced with smaller value.

unsigned int aBegin = 25;
unsigned int aEnd = 35; // works if 35 is replaced with 34.

vectorList.insert(vectorList.begin() + 35, // works if 35 is
replaced by 36, 37, 38 ...
// but not if 35 is replaced by 34, 33, 32...
vectorList.begin() + aBegin, vectorList.begin() + aEnd);

return 0;


When this is run, an error occurs during the insertion about
incompatible vector iterators.
At first I thought, maybe this is happening because I'm trying to
insert into the range that I am copying...but, if the size of the
vector is just changed from 45 elements long to 44 elements long...the
program works! I think that is just bizarre.

Does anybody understand what is going on here? It would be crazy to
write code that might fail whenever it encounters a vector.insert() so
I would like to understand the reason this code fails but when the 45
is replaced with 44, it works.

Any insight appreciated!


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
can someone explain line 7 and 8 of the insertion sort Troy Java 0 02-16-2008 04:50 PM
Bizarre vector insertion behavior - can someone please enlighten? C++ 9 03-08-2007 02:26 AM
Free memory allocate by a STL vector, vector of vector, map of vector C++ 8 02-18-2006 12:48 AM
Bizarre behavior by FireFox 1.0.7 Gerald Orvis Davis Firefox 2 11-07-2005 07:53 AM
Can someone please confirm this behavior? Aaron Bertrand - MVP ASP General 7 12-18-2003 04:59 PM