Iterator problem

F

fdm

In a template function I have this code:



....
....
typedef V::ValueType ValueType;


std::vector<ValueType> vecTmp(5);
for (int i=0; i <5; i++) {
vecTmp = paramCurrent;
}

std::vector<ValueType>::iterator it = vecTmp.begin();
for (int j=0; j<2; j++) {
ValueType val = deform[j];
vecTmp.insert(it, val);
}


But when I try to insert an element where the "it" iterator is pointing I
get the error:

unknown location(0): fatal error in "test_array": G:\Programs\Microsoft
Visual Studio 9.0\VC\include\vector(251) : Assertion failed: vector
iterators incompatible

I am using VS 2008 on vista 64. I cannot see the problem. Both the vector
and the iterator are intialized with the ValueType. Any ideas?
 
V

Victor Bazarov

fdm said:
In a template function I have this code:



...
...
typedef V::ValueType ValueType;


std::vector<ValueType> vecTmp(5);
for (int i=0; i <5; i++) {
vecTmp = paramCurrent;


So, I take it 'paramCurrent' is an array of 'V::ValueType'.
}

std::vector<ValueType>::iterator it = vecTmp.begin();
for (int j=0; j<2; j++) {
ValueType val = deform[j];

OK, so 'deform' is also an array of 'V::ValueType'.
vecTmp.insert(it, val);
}


But when I try to insert an element where the "it" iterator is pointing
How?

I get the error:

unknown location(0): fatal error in "test_array": G:\Programs\Microsoft
Visual Studio 9.0\VC\include\vector(251) : Assertion failed: vector
iterators incompatible

I don't see 'test_array' anywhere your example.
I am using VS 2008 on vista 64. I cannot see the problem. Both the
vector and the iterator are intialized with the ValueType.

That's nonsense. An iterator can only be initialised with an iterator.
> Any ideas?

FAQ 5.8. Read it and follow the recommendations.


V
 
F

fdm

This works:


std::vector<ValueType>::iterator it = vecTmp.begin();
vecTmp.insert(it, deform[0]);


But this gives the previous error:


std::vector<ValueType>::iterator it = vecTmp.begin();
for (int j=0; j<spaceDim; j++) {
ValueType val = deform[j];
vecTmp.insert(it, val);
}


For some reason its only possible to use 'insert' outside a loop - makes no
sense to me.




Victor Bazarov said:
fdm said:
In a template function I have this code:



...
...
typedef V::ValueType ValueType;


std::vector<ValueType> vecTmp(5);
for (int i=0; i <5; i++) {
vecTmp = paramCurrent;


So, I take it 'paramCurrent' is an array of 'V::ValueType'.
}

std::vector<ValueType>::iterator it = vecTmp.begin();
for (int j=0; j<2; j++) {
ValueType val = deform[j];

OK, so 'deform' is also an array of 'V::ValueType'.
vecTmp.insert(it, val);
}


But when I try to insert an element where the "it" iterator is pointing
How?

I get the error:

unknown location(0): fatal error in "test_array": G:\Programs\Microsoft
Visual Studio 9.0\VC\include\vector(251) : Assertion failed: vector
iterators incompatible

I don't see 'test_array' anywhere your example.
I am using VS 2008 on vista 64. I cannot see the problem. Both the vector
and the iterator are intialized with the ValueType.

That's nonsense. An iterator can only be initialised with an iterator.
Any ideas?

FAQ 5.8. Read it and follow the recommendations.


V
 
F

fdm

Forgot about the fact that the iterator is invalidated. This solves the
problem:


for (int j=0; j<spaceDim; j++) {
ValueType val = deform[j];
vecTmp.insert(it, val);
it = vecTmp.begin();
}



fdm said:
This works:


std::vector<ValueType>::iterator it = vecTmp.begin();
vecTmp.insert(it, deform[0]);


But this gives the previous error:


std::vector<ValueType>::iterator it = vecTmp.begin();
for (int j=0; j<spaceDim; j++) {
ValueType val = deform[j];
vecTmp.insert(it, val);
}


For some reason its only possible to use 'insert' outside a loop - makes
no sense to me.




Victor Bazarov said:
fdm said:
In a template function I have this code:



...
...
typedef V::ValueType ValueType;


std::vector<ValueType> vecTmp(5);
for (int i=0; i <5; i++) {
vecTmp = paramCurrent;


So, I take it 'paramCurrent' is an array of 'V::ValueType'.
}

std::vector<ValueType>::iterator it = vecTmp.begin();
for (int j=0; j<2; j++) {
ValueType val = deform[j];

OK, so 'deform' is also an array of 'V::ValueType'.
vecTmp.insert(it, val);
}


But when I try to insert an element where the "it" iterator is pointing
How?

I get the error:

unknown location(0): fatal error in "test_array": G:\Programs\Microsoft
Visual Studio 9.0\VC\include\vector(251) : Assertion failed: vector
iterators incompatible

I don't see 'test_array' anywhere your example.
I am using VS 2008 on vista 64. I cannot see the problem. Both the
vector and the iterator are intialized with the ValueType.

That's nonsense. An iterator can only be initialised with an iterator.
Any ideas?

FAQ 5.8. Read it and follow the recommendations.


V
 
T

Thomas J. Gritzan

fdm said:
This works:


std::vector<ValueType>::iterator it = vecTmp.begin();
vecTmp.insert(it, deform[0]);


But this gives the previous error:


std::vector<ValueType>::iterator it = vecTmp.begin();
for (int j=0; j<spaceDim; j++) {
ValueType val = deform[j];
vecTmp.insert(it, val);
}


For some reason its only possible to use 'insert' outside a loop - makes
no sense to me.

Insert invalidates iterators, because it might cause a reallocation. So
you have to get the current begin() every time you insert a value, or
avoid reallocation by reserving enough space in the vector.

You could also use the pair-of-iterator forms of the constructor and
insert to avoid the explicit loops:

typedef V::ValueType ValueType;

// initialize vecTmp with paramCurrent[0..4]
std::vector<ValueType> vecTmp( &paramCurrent[0], &paramCurrent[5] );

// insert deform[0..1] at the beginning
vecTmp.insert( vecTmp.begin(), &deform[0], &deform[2] );
 
F

Francesco S. Carta

Forgot about the fact that the iterator is invalidated. This solves the
problem:

    for (int j=0; j<spaceDim; j++) {
      ValueType val = deform[j];
      vecTmp.insert(it, val);
      it = vecTmp.begin();
    }

Nice to see that you solved your issue.

Will you accept an advice?

Don't top-post. It's truly a bad habit when posting to newsgroups and
alike, some users will not reply to your posts just because they're
top-posted.

About the code, you might avoid to use the "it" iterator as a separate
variable, you could call begin() straight inside of the insert() call,
but if it were my code, I would reserve vector's space upfront and I
would add elements to the tail. Eventually, I would reverse the whole
vector (or part of it) later.

Do you guess the reason?

Have good coding,
Francesco
 
B

Brian Wood

fdm schrieb:




This works:
   std::vector<ValueType>::iterator it = vecTmp.begin();
   vecTmp.insert(it,  deform[0]);
But this gives the previous error:
  std::vector<ValueType>::iterator it = vecTmp.begin();
  for (int j=0; j<spaceDim; j++) {
    ValueType val = deform[j];
     vecTmp.insert(it, val);
  }
For some reason its only possible to use 'insert' outside a loop - makes
no sense to me.

Insert invalidates iterators, because it might cause a reallocation. So
you have to get the current begin() every time you insert a value, or
avoid reallocation by reserving enough space in the vector.

Another option would be to use a stable_vector --

http://bannalia.blogspot.com/2008/09/introducing-stablevector.html

http://svn.boost.org/svn/boost/sandbox/move/boost/container/stable_vector.hpp

http://webEbenezer.net/misc/stable_vector.hpp


Brian Wood
http://webEbenezer.net
 
M

Michael Doubez

fdm schrieb:
This works:
   std::vector<ValueType>::iterator it = vecTmp.begin();
   vecTmp.insert(it,  deform[0]);
But this gives the previous error:
  std::vector<ValueType>::iterator it = vecTmp.begin();
  for (int j=0; j<spaceDim; j++) {
    ValueType val = deform[j];
     vecTmp.insert(it, val);
  }
For some reason its only possible to use 'insert' outside a loop - makes
no sense to me.
Insert invalidates iterators, because it might cause a reallocation. So
you have to get the current begin() every time you insert a value, or
avoid reallocation by reserving enough space in the vector.

Another option would be to use a stable_vector --

Another option would be to drop iterator usage and use a list<> or a
deque<> with push_front().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top