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. Managed C++/CLI
  4. Refreshing the iterator

Refreshing the iterator

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
3 Posts 2 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.
  • P Offline
    P Offline
    Pazzuzu
    wrote on last edited by
    #1

    Hi All, To demonstrate my problem,Have put in a small piece of code below : Iam putting some strings into my list & while iterating the list,want to remove a particular element from the list.The following code crashes when I iterate further. I know that I have to refresh my iterator somehow but how?. How could I get around this problem? Thanks.. list pool; pool.push_back("PageType1"); pool.push_back("PageType2"); pool.push_back("PageType3"); pool.push_back("PageType4"); list::iterator iter = pool.begin(); while(iter!=pool.end()) { long size = pool.size(); if(*iter=="PageType2") pool.remove("PageType2"); iter++; }

    C 1 Reply Last reply
    0
    • P Pazzuzu

      Hi All, To demonstrate my problem,Have put in a small piece of code below : Iam putting some strings into my list & while iterating the list,want to remove a particular element from the list.The following code crashes when I iterate further. I know that I have to refresh my iterator somehow but how?. How could I get around this problem? Thanks.. list pool; pool.push_back("PageType1"); pool.push_back("PageType2"); pool.push_back("PageType3"); pool.push_back("PageType4"); list::iterator iter = pool.begin(); while(iter!=pool.end()) { long size = pool.size(); if(*iter=="PageType2") pool.remove("PageType2"); iter++; }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      What you need to do is build a list of items to remove, then remove them all in another loop. You can't remove items from a list, because that item contains the pointer to the next item. Christian Graus - Microsoft MVP - C++

      P 1 Reply Last reply
      0
      • C Christian Graus

        What you need to do is build a list of items to remove, then remove them all in another loop. You can't remove items from a list, because that item contains the pointer to the next item. Christian Graus - Microsoft MVP - C++

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

        Will try that.Thanks...

        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