C
ckfan.painter
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"
#include<vector>
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.
vectorList.push_back(i);
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!
CK
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"
#include<vector>
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.
vectorList.push_back(i);
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!
CK