Erasing a vector<vector inside a for-loop
-
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
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
-
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
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), thenbreak
instead ofreturn
. -- The Blog: Bits and Pieces