0

Is something wrong with erasing with reverse iterator? When compiling the code fragment below, I receive 'no matching function' error when using 'rit' as argument for erase().

std::vector<MyRecord*>* BLV = GetVector;

for (std::vector<MyRecord*>::iterator it = BLV->begin(); it != BLV->end(); ++it)
  BLV->erase(it);
for (std::vector<MyRecord*>::reverse_iterator rit = BLV->rbegin(); rit != BLV->rend(); ++rit)
  BLV->erase(rit);
katang
  • 2,474
  • 5
  • 24
  • 48
  • Make a [mcve] so not everyone reading your question has to do it over and over again. – nwp Jun 16 '16 at 08:33
  • @nwp I only asked 'What is wrong', which I guessed needs only careful reading, rather than running an MWE. – katang Jun 16 '16 at 08:53

1 Answers1

3

Indeed erase cannot be used directly with a reverse iterator; you'd essentially be removing the wrong element if it were allowed.

You need to convert rit to a forward iterator.

(rit + 1).base(); will give you the equivalent it. Note carefully the + 1.

Putting this together, write

BLV->erase((rit + 1).base());

in your reverse case.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483