How do I clear the std::queue efficiently?

后端 未结 11 962
醉梦人生
醉梦人生 2020-12-12 09:10

I am using std::queue for implementing JobQueue class. ( Basically this class process each job in FIFO manner). In one scenario, I want to clear the queue in one shot( delet

11条回答
  •  死守一世寂寞
    2020-12-12 09:40

    Using a unique_ptr might be OK.
    You then reset it to obtain an empty queue and release the memory of the first queue. As to the complexity? I'm not sure - but guess it's O(1).

    Possible code:

    typedef queue quint;
    
    unique_ptr p(new quint);
    
    // ...
    
    p.reset(new quint);  // the old queue has been destroyed and you start afresh with an empty queue
    

提交回复
热议问题