a RT error with iterators

Hi All,

my program returns a run time error and I'd be pleased if you help me to figure up the problem.

I have a vector of pointers to myClass (which is sorted due to myClass.value) and I'd like to insert a new object instead of the element in the vector with the biggest value.

like this:
while a new value arrives (myClass1.value < newVal < myClass2.value), the last element (myClass3) is removed and a new myClass is inserted with its newVal at the right position in the sorted list (between myClass1 and myClass2).


myVector (a sorted vector due to the field: value)
|
|->myClass1__
| |__value
|
|->myClass2__
| |__value
|
|->myClass3__
|__value


I got the RT error:
---------------------
1
2
Debug Assertion Failed
Expression: vector iterators incompatible


my code:
-----------
1
2
3
4
5
delete myVector.back();
myVector.pop_back();
myClass* c_low = new myClass(cur_val); // ctor of myClass
myVector.insert(it1,c_low);
break;



I guess it's an allocation issue ...
May you have an idea?

Thanks in advance,
Hila
Last edited on
The code seems fine to me, although in general you should be careful about invalidating iterators by changing the container.

Can you show how it1 was set up?
I have a vector of pointers to myClass
¿Is there a good reason for that?
Thank you all for reply,

kbw, this is for you q:
1
2
3
4
5
6
7
8
vector<risk*>::iterator it1;
for(it1 = myVector.begin(); it1< myVector.end(); it1++)
{
        if(cur_val<=(*it1)->getVal())
	{
         //the code I posted
         }
}


ne555,
please offer your idea, I used a vector of pointers to myClass in order to maintain some elements of myClass. do you suggest to use a vector of myClass rather then a vector of pointers to myClass? if yes, how the code looks like?
Last edited on
pop_back invalidates any iterator referring to myVector.end()-1, which will happen.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	vector<risk*>::iterator it1;
	for(it1 = myVector.begin(); it1< myVector.end(); it1++)
	{
		if(cur_val<=(*it1)->getVal())
		{
			if ( it1 == myVector.end()-1)
			{
				delete *it1 ;
				*it1 = new myClass(cur_val);
			}
			else
			{
				delete myVector.back() ;
				myVector.pop_back() ;
				myVector.insert(it1, new myClass(cur_val)) ;
			}
			break ;		
		}
	}


The same, just change the type of the vector.
1
2
std::vector<risk> myVector;
myVector.insert( it, risk(cur_value) ); //now inserting objects 
You will not delete anything here, there will be no memory leaks, you only need to dereference the iterators once.
However insert and erase will cost you more if the class is big (although if you do that a lot maybe a list is better)
The loop ought to be:
1
2
3
4
5
6
7
8
vector<risk*>::iterator it1;
for(it1 = myVector.begin(); it1 != myVector.end(); ++it1)
{
	if(cur_val<=(*it1)->getVal())
	{
		//the code I posted
	}
}


Test for equality, not less than.
Last edited on
This works:
1
2
3
4
5
6
7
8
9
10
11
12
vector<myClass> myVector; 
vector<myClass>::iterator it;
	
for(it = myVector.begin(); it < myVector.end()-1; ++it)
{
	if(c_low.getRiskVal() <= (*it).getRiskVal())
	{
		myVector.pop_back();
		myVector.insert (it,c_low);
		break;
	}
}
Last edited on
@kbw: with Random Access Iterators, why does it matter?
with Random Access Iterators, why does it matter?
For a vector, it doesn't matter.
Topic archived. No new replies allowed.