简体   繁体   中英

Merge two std::queue

Is there any function in stl that joins two std::queue objects?

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 splice d 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM