Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erase multiple elements in deque C++

I was trying to erase multiple elements in deque in the 'while' loop below, but my program does not give the correct results.

Problem Background:

packet_deque contains 9 Packet objects - some of them are sent and the others are not sent.

#include <deque> 
#include <iostream> 

class Packet
{
public:
    void set_sent() { sent = true; };
    bool get_sent() { return sent; };
private:
    /* members of Test class */
    bool sent = false;
};

int main()
{ 
    size_t MAX = 9;
    std::deque<Packet> packet_deque(MAX);
    unsigned int i = 0;

    std::deque<Packet>::iterator itr = packet_deque.begin();
    printf("Before erasing sent packets - packet_dequeue size: %d \n", packet_deque.size());
    // initialise the packet queue - creating some sent packets
    for (; itr != packet_deque.end(); ++itr) 
    {
        // set some sent packets
        if (i % 3 == 0) 
        {
            itr->set_sent();
        }
        printf("packet_deque[%d]: %s\n", i, itr->get_sent() ? "sent" : "not_sent");
        ++i;
    }
    printf("\n");

    // erase sent packets in packet_dequeue
    itr = packet_deque.begin();
    while (itr != packet_deque.end())
    {
        if (itr->get_sent())
        {
            // erase the element and do NOT move the pointer
            packet_deque.erase(itr);
        }
        else
        {
            // move to next element
            ++itr;
        }
    }

    printf("After erasing sent packets - packet_dequeue size: %d \n", packet_deque.size());
    for (itr = packet_deque.begin(), i = 0; itr != packet_deque.end(); ++itr) 
    {
        printf("packet_deque[%d]: %s\n", i, itr->get_sent() ? "sent" : "not_sent");
        ++i;
    }
} 

The terminal output is:

Before erasing sent packets - packet_dequeue size: 9 
packet_deque[0]: sent
packet_deque[1]: not_sent
packet_deque[2]: not_sent
packet_deque[3]: sent
packet_deque[4]: not_sent
packet_deque[5]: not_sent
packet_deque[6]: sent
packet_deque[7]: not_sent
packet_deque[8]: not_sent

After erasing sent packets - packet_dequeue size: 5 
packet_deque[0]: not_sent
packet_deque[1]: not_sent
packet_deque[2]: not_sent
packet_deque[3]: not_sent
packet_deque[4]: not_sent

Question:

Since the while loop just removes sent packets, I expect 6 unsent packets, but only got 5. I couldn't figure out what went wrong... Why the final result only contains 5 unsent packets instead of 6 unsent packets?

like image 913
ZuckerReis Avatar asked Sep 01 '26 11:09

ZuckerReis


2 Answers

In general all iterators are invalidated after an erase from a deque. Your code uses the invalidated iterator in the while condition.

However erase returns a new valid iterator pointing to the element immediately after the erased one, so the correct code is

itr = packet_deque.erase(itr);
like image 73
john Avatar answered Sep 03 '26 01:09

john


Use

itr = packet_deque.erase(itr);

Otherwise the current iterator is invalid.

like image 37
Vlad from Moscow Avatar answered Sep 03 '26 01:09

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!