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. Iterator Invalidation? iVec.erase(it)

Iterator Invalidation? iVec.erase(it)

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphics
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.
  • Y Offline
    Y Offline
    Yonggoo
    wrote on last edited by
    #1

    Hi all, I am having runtime crash with code below int main(int argc, char** argv) { vector iVec; iVec.push_back(0); iVec.push_back(10); iVec.push_back(20); iVec.push_back(30); iVec.push_back(40); iVec.push_back(30); iVec.push_back(60); vector::iterator it; for(it=iVec.begin(); it != iVec.end();++it) { if(*it = 30) { iVec.erase(it); } --it; } return 0; } How can I get all "30" out? Thanks!

    Yonggoo

    T P 2 Replies Last reply
    0
    • Y Yonggoo

      Hi all, I am having runtime crash with code below int main(int argc, char** argv) { vector iVec; iVec.push_back(0); iVec.push_back(10); iVec.push_back(20); iVec.push_back(30); iVec.push_back(40); iVec.push_back(30); iVec.push_back(60); vector::iterator it; for(it=iVec.begin(); it != iVec.end();++it) { if(*it = 30) { iVec.erase(it); } --it; } return 0; } How can I get all "30" out? Thanks!

      Yonggoo

      T Offline
      T Offline
      tanvon malik
      wrote on last edited by
      #2

      for(it=iVec.begin(); it != iVec.end();++it) { if(*it = 30) // May be this is the problem { iVec.erase(it); } --it; } the above if line is the culprit if you typed it so in your code also it should be if(*it == 30) // corrected

      Tanvon the brain behind ... I Blog here

      1 Reply Last reply
      0
      • Y Yonggoo

        Hi all, I am having runtime crash with code below int main(int argc, char** argv) { vector iVec; iVec.push_back(0); iVec.push_back(10); iVec.push_back(20); iVec.push_back(30); iVec.push_back(40); iVec.push_back(30); iVec.push_back(60); vector::iterator it; for(it=iVec.begin(); it != iVec.end();++it) { if(*it = 30) { iVec.erase(it); } --it; } return 0; } How can I get all "30" out? Thanks!

        Yonggoo

        P Offline
        P Offline
        prg_desh
        wrote on last edited by
        #3

        I'm not getting any crash with your code ?

        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