开发者

Merge two std::queue

开发者 https://www.devze.com 2023-03-02 14:23 出处:网络
Is there any function in stl that joins two std::queue obj开发者_如何学Cects?The std::queue adapter doesn\'t support iteration so you\'d actually have to roll your own method to do this. But given tha

Is there any function in stl that joins two std::queue obj开发者_如何学Cects?


The std::queue adapter doesn't support iteration so you'd actually have to roll your own method to do this. But given that you need this functionality, you should probably consider a different container. If you need random access, the probably std::deque. If you only need front/back access like a queue consider std::list which can be spliced together in constant time.


There does not seem to be any options provided in stl, but I can think of some other things you can write yourself:

  1. Write your own code to read one queue into another, but this is O(n).

  2. Use std::copy to manipulate the underlying std::deque containers, again O(n).

  3. Create your own container which is implemented in terms of std::queue, but can maintain multiple queues to simulate a join in O(1).

0

精彩评论

暂无评论...
验证码 换一张
取 消