How do I clear the std::queue efficiently?

后端 未结 11 983
醉梦人生
醉梦人生 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:37

    A common idiom for clearing standard containers is swapping with an empty version of the container:

    void clear( std::queue &q )
    {
       std::queue empty;
       std::swap( q, empty );
    }
    

    It is also the only way of actually clearing the memory held inside some containers (std::vector)

提交回复
热议问题