Share via

How to using boost_auto?

D.D.K-2637 966 Reputation points
Aug 24, 2021, 11:43 AM

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!

C++
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.
3,891 questions
{count} votes

Accepted answer
  1. Igor Tandetnik 1,106 Reputation points
    Aug 24, 2021, 1:01 PM

    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.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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