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. Erasing a vector<vector inside a for-loop

Erasing a vector<vector inside a for-loop

Scheduled Pinned Locked Moved C / C++ / MFC
c++comgraphicshelpquestion
8 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.
  • J Offline
    J Offline
    jc0dex
    wrote on last edited by
    #1

    Hey guys, Alright I've got a simple double for-loop here using the stl's vector. // go thru the vector of Critical Packet lists for(int i=0; i<_PacketLists.size(); i++) { // go thru the specified Critical Packet list for(int j=0; j<_PacketLists[i].size();j++) { if(_PacketLists[i][j].seqNumber == seqNum) { Erase Vector here ...... } } } The vector class has an erase function, but it only takes iterators. Is there any way to get around using iterators?I've tried using them with a vector inside a vector and has proven troublesome to me in the past. Any help would be much appreciated!!! Thanks in advance, Jay www.jdaigner.com jay@jdaigner.com

    L J 2 Replies Last reply
    0
    • J jc0dex

      Hey guys, Alright I've got a simple double for-loop here using the stl's vector. // go thru the vector of Critical Packet lists for(int i=0; i<_PacketLists.size(); i++) { // go thru the specified Critical Packet list for(int j=0; j<_PacketLists[i].size();j++) { if(_PacketLists[i][j].seqNumber == seqNum) { Erase Vector here ...... } } } The vector class has an erase function, but it only takes iterators. Is there any way to get around using iterators?I've tried using them with a vector inside a vector and has proven troublesome to me in the past. Any help would be much appreciated!!! Thanks in advance, Jay www.jdaigner.com jay@jdaigner.com

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

      There is a clear() method that will erase all vector contents.

      1 Reply Last reply
      0
      • J jc0dex

        Hey guys, Alright I've got a simple double for-loop here using the stl's vector. // go thru the vector of Critical Packet lists for(int i=0; i<_PacketLists.size(); i++) { // go thru the specified Critical Packet list for(int j=0; j<_PacketLists[i].size();j++) { if(_PacketLists[i][j].seqNumber == seqNum) { Erase Vector here ...... } } } The vector class has an erase function, but it only takes iterators. Is there any way to get around using iterators?I've tried using them with a vector inside a vector and has proven troublesome to me in the past. Any help would be much appreciated!!! Thanks in advance, Jay www.jdaigner.com jay@jdaigner.com

        J Offline
        J Offline
        Johann Gerell
        wrote on last edited by
        #3

        I took the liberty of rewriting your vague snippet:

        for(int i = 0; i < _PacketLists.size(); ++i)
        {
        PacketList& packetList = _PacketLists[i];

        for(int j = 0; j < packetList.size(); ++j)
        {
            Packet& packet = packetList\[j\];
        
            if(packet.seqNumber == seqNum)
            {
                // Remove the packet.
            }
        }
        

        }

        If that's what you meant, then this is how I would have done it from the beginning:

        for(std::vector::iterator listIt = _PacketLists.begin(); listIt != _PacketLists.end(); ++listIt)
        {
        PacketList& packetList = *listIt;

        for(PacketList::iterator packetIt = packetList.begin(); packetIt != packetList.end(); ++packetIt)
        {
            Packet& packet = \*packetIt;
        
            if(packet.seqNumber == seqNum)
            {
                packetList.erase(packetIt);
        
                return;
            }
        }
        

        }

        jc0dex wrote:

        Is there any way to get around using iterators

        Why should you? -- The Blog: Bits and Pieces

        J 1 Reply Last reply
        0
        • J Johann Gerell

          I took the liberty of rewriting your vague snippet:

          for(int i = 0; i < _PacketLists.size(); ++i)
          {
          PacketList& packetList = _PacketLists[i];

          for(int j = 0; j < packetList.size(); ++j)
          {
              Packet& packet = packetList\[j\];
          
              if(packet.seqNumber == seqNum)
              {
                  // Remove the packet.
              }
          }
          

          }

          If that's what you meant, then this is how I would have done it from the beginning:

          for(std::vector::iterator listIt = _PacketLists.begin(); listIt != _PacketLists.end(); ++listIt)
          {
          PacketList& packetList = *listIt;

          for(PacketList::iterator packetIt = packetList.begin(); packetIt != packetList.end(); ++packetIt)
          {
              Packet& packet = \*packetIt;
          
              if(packet.seqNumber == seqNum)
              {
                  packetList.erase(packetIt);
          
                  return;
              }
          }
          

          }

          jc0dex wrote:

          Is there any way to get around using iterators

          Why should you? -- The Blog: Bits and Pieces

          J Offline
          J Offline
          jc0dex
          wrote on last edited by
          #4

          Yea my snippet was very vague I apologize. I have a vector _PacketList ... Im not sure how to loop thru the outside _Packetlist, and for each outside _Packetlist loop through all of it's packets. Inside that loop is where I need to check each individual packet's sequence number against the one passed in. I hope this clarifies things because the sample code given doesn't really match what I'm trying to do. And why the attitude :-P

          J 1 Reply Last reply
          0
          • J jc0dex

            Yea my snippet was very vague I apologize. I have a vector _PacketList ... Im not sure how to loop thru the outside _Packetlist, and for each outside _Packetlist loop through all of it's packets. Inside that loop is where I need to check each individual packet's sequence number against the one passed in. I hope this clarifies things because the sample code given doesn't really match what I'm trying to do. And why the attitude :-P

            J Offline
            J Offline
            Johann Gerell
            wrote on last edited by
            #5

            jc0dex wrote:

            Im not sure how to loop thru the outside _Packetlist, and for each outside _Packetlist loop through all of it's packets

            I showed you how to do it in my previous post. :^)

            jc0dex wrote:

            And why the attitude :-P

            What? -- The Blog: Bits and Pieces

            J 1 Reply Last reply
            0
            • J Johann Gerell

              jc0dex wrote:

              Im not sure how to loop thru the outside _Packetlist, and for each outside _Packetlist loop through all of it's packets

              I showed you how to do it in my previous post. :^)

              jc0dex wrote:

              And why the attitude :-P

              What? -- The Blog: Bits and Pieces

              J Offline
              J Offline
              jc0dex
              wrote on last edited by
              #6

              for(std::vector::iterator listIt = _PacketLists.begin(); listIt != _PacketLists.end(); ++listIt) { PacketList& packetList = *listIt; for(PacketList::iterator packetIt = packetList.begin(); packetIt != packetList.end(); ++packetIt) { Packet& packet = *packetIt; if(packet.seqNumber == seqNum) { packetList.erase(packetIt); return; } } } - What exactly is PacketList representing? I have a vector<_PACKETS> inside another vector called _PacketLists. (_PACKETS is a small struct) That's the critical piece I'm not understanding to this problem. Would it be like vector<_PACKET>&packetlist = *listIt? And then I could use packetlist.begin() to iterate thru the other list? I'm a still little confused tho .. thanks for all the help

              J J 2 Replies Last reply
              0
              • J jc0dex

                for(std::vector::iterator listIt = _PacketLists.begin(); listIt != _PacketLists.end(); ++listIt) { PacketList& packetList = *listIt; for(PacketList::iterator packetIt = packetList.begin(); packetIt != packetList.end(); ++packetIt) { Packet& packet = *packetIt; if(packet.seqNumber == seqNum) { packetList.erase(packetIt); return; } } } - What exactly is PacketList representing? I have a vector<_PACKETS> inside another vector called _PacketLists. (_PACKETS is a small struct) That's the critical piece I'm not understanding to this problem. Would it be like vector<_PACKET>&packetlist = *listIt? And then I could use packetlist.begin() to iterate thru the other list? I'm a still little confused tho .. thanks for all the help

                J Offline
                J Offline
                jc0dex
                wrote on last edited by
                #7

                Oh and also what would be the template type of the vector in the first for-loop? A vector > ?

                1 Reply Last reply
                0
                • J jc0dex

                  for(std::vector::iterator listIt = _PacketLists.begin(); listIt != _PacketLists.end(); ++listIt) { PacketList& packetList = *listIt; for(PacketList::iterator packetIt = packetList.begin(); packetIt != packetList.end(); ++packetIt) { Packet& packet = *packetIt; if(packet.seqNumber == seqNum) { packetList.erase(packetIt); return; } } } - What exactly is PacketList representing? I have a vector<_PACKETS> inside another vector called _PacketLists. (_PACKETS is a small struct) That's the critical piece I'm not understanding to this problem. Would it be like vector<_PACKET>&packetlist = *listIt? And then I could use packetlist.begin() to iterate thru the other list? I'm a still little confused tho .. thanks for all the help

                  J Offline
                  J Offline
                  Johann Gerell
                  wrote on last edited by
                  #8

                  Assuming this is what you have:

                  typedef std::vector< _PACKETS > PacketList;
                  std::vector< PacketList > _PacketLists;

                  then this is probably what you want:

                  for(std::vector< PacketList >::iterator listIt = _PacketLists.begin(); listIt != _PacketLists.end(); ++listIt)
                  {
                  PacketList& packetList = *listIt;

                  for(PacketList::iterator packetIt = packetList.begin(); packetIt != packetList.end(); ++packetIt)
                  {
                      \_PACKETS& packets = \*packetIt;
                  
                      if(packets.seqNumber == seqNum)
                      {
                          packetList.erase(packetIt);
                  
                          return;
                      }
                  }
                  

                  }

                  which simply means that you iterate over all elements of the outer vector, and for each inner vector, all elements is iterated until the one matching the seqNum criteria is found. It's then erased from that inner vector and the iteration is stopped. If you want to continue searching the remaining inner vectors (which I'd guess), then break instead of return. -- The Blog: Bits and Pieces

                  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