Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. deleting from a vector

deleting from a vector

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++graphicstutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    I wrote some C++ code to create a vector and then try to delete all occurrences of 3 in it:

    vector myVector = { 1, 2, 3, 4, 5, 3, 2 };

    for (auto i = myVector.begin(); i != myVector.end(); i++)
    {
    	if (\*i == 3) myVector.erase(i);
    }
    

    However, it doesn't work right. It gives this error: Expression: can't increment invalidated vector iterator I guess you can't increment the iterator after you erase the element. Anyone know how to fix this? Thanks.

    L M 2 Replies Last reply
    0
    • M mike7411

      I wrote some C++ code to create a vector and then try to delete all occurrences of 3 in it:

      vector myVector = { 1, 2, 3, 4, 5, 3, 2 };

      for (auto i = myVector.begin(); i != myVector.end(); i++)
      {
      	if (\*i == 3) myVector.erase(i);
      }
      

      However, it doesn't work right. It gives this error: Expression: can't increment invalidated vector iterator I guess you can't increment the iterator after you erase the element. Anyone know how to fix this? Thanks.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The problem comes because as soon as you remove an element then all the rest effectively move nearer the beginning, and thus their positions are out of sync with the iterator. You could try a reverse iterator as detailed at std::vector - cppreference.com[^].

      1 Reply Last reply
      0
      • M mike7411

        I wrote some C++ code to create a vector and then try to delete all occurrences of 3 in it:

        vector myVector = { 1, 2, 3, 4, 5, 3, 2 };

        for (auto i = myVector.begin(); i != myVector.end(); i++)
        {
        	if (\*i == 3) myVector.erase(i);
        }
        

        However, it doesn't work right. It gives this error: Expression: can't increment invalidated vector iterator I guess you can't increment the iterator after you erase the element. Anyone know how to fix this? Thanks.

        M Offline
        M Offline
        Mircea Neacsu
        wrote on last edited by
        #3

        As you guessed, erase invalidates all iterators on the container. Your problem is known as the Erase–remove idiom[^] There are different ways of solving it: 1. The "manual" way:

        auto pos = myVector.begin();
        size_t i=0;
        while (pos != myVector.end())
        {
        if (*pos ==3)
        {
        myVector.erase(pos);
        pos = myVector.begin() + i;
        }
        else
        {
        ++i;
        ++pos;
        }
        }

        2. The "semi-automatic" way:

        auto pos = myVector.begin();
        while (pos != myVector.end())
        {
        if (*pos ==3)
        pos = myVector.erase(pos); //erase returns an iterator pointing to next element
        else
        ++pos;
        }

        3. The "old fully automatic way":

        myVector.erase(std::remove(myVector.begin(),
        myVector.end(), 3),
        myVector.end());

        4. The "new fully automatic way" (since C++20):

        std::erase (myVector, 3);

        Disclaimer: I didn't compile any of the code above; some errors may/will exist :)

        Mircea

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups