6

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

2
  • Do you really need to use a queue? If you use an std::list instead you could splice the two lists in constant time (that's assuming that they don't need to be sorted).
    – GWW
    May 2, 2011 at 20:45
  • 2
    What would it mean to do so? How would entries be ordered in the merged queue?
    – user2100815
    May 2, 2011 at 20:52

2 Answers 2

6

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.

1
  • 5
    Except using a list will only be beneficial if he splices large lists often, because a list is much slower doing everything else.
    – GManNickG
    May 2, 2011 at 20:56
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.