Share via

How to using boost_auto?

D.D.K-2637 966 Reputation points
2021-08-24T11:43:33.193+00:00

Hi,
I still haven't found a direct efficient way with this code:
The problem is that after removing the vector element, boost_auto throws an error :

vector iterators incompatible and then the my program crashed.

for (BOOST_AUTO(x, A.begin()); x != (A.end()); ) {  
  if (_checksomething) {  
++x;  
  }  
  else {  
   A.erase(x);  
--x;  
  }  
}  

if add code: --x;
126035-image.png

I could avoid with a copy array or save the index but I think it's temporary and maybe longer..
Is there other way to fix this?

Thanks you!

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Answer accepted by question author

Igor Tandetnik 1,116 Reputation points
2021-08-24T13:01:31.443+00:00

You likely want x = A.erase(x). vector::erase returns an iterator to the element following the one that's erased.

The reason A.erase(x); --x; doesn't work is because erase(x) invalidates the iterator. The only thing you can do with x afterwards is assign a new value to it.

Was this answer helpful?

2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.