Vector of queues

16,722

Solution 1

vector<queue<int>> vec; // vector of queues
vec.push_back(queue<int>()); // add a queue
vec[0].push(1); // push 1 into queue number 0.

Solution 2

You can do something like this:

int main( void )
{
    typedef std::queue<int> Q;
    std::vector<Q> v;

    //Add a new queue to vector
    v.push_back(Q());

    //Add an element to the queue
    v[0].push(1);
    return 0;
}

Solution 3

typedef std::queue<int> IntQueue;
typedef std::vector<IntQueue> IntQueueVector
IntQueueVector myVector;

1)

myVector.push_back(IntQueue());

2)

myVector.back().push(1);

Solution 4

queue has the semantics that allow it to be used in std::vector, so you can just use it like any other vector when you come to add, eg use push_back to add a queue to the vector.

Inserting into a queue is with push() as you can only push into one end. You can access the queue through operator[] eg queuevec[i], where i is the number of the queue you wish to use.

If this is being used in a multi-threading context it is safe for two different threads to access two different queues within the vector without locking, but it is not safe for two threads to access the same queue. If you add a queue to the vector this may invalidate all the other queues in the vector during this period if they are being "moved", therefore you would need to lock your mutex to perform this action.

Share:
16,722
XCS
Author by

XCS

Algorithms are fun. UX is sublime. JavaScript is the one. Ping-pong to pass time. userTrack.net - Self Hosted Analytics Platform

Updated on June 04, 2022

Comments

  • XCS
    XCS almost 2 years

    How to create vector of queues, and how do I add elements?

    I want to be able to do the following:
    -insert a new queue into the vector
    -insert a new element into a queue that's inside the vector.

    :D

  • XCS
    XCS over 13 years
    What's the typedef useful for?:D Is it better then: "vector<queue<int>> vec;"?
  • Moo-Juice
    Moo-Juice over 13 years
    The typedef allows you to specify a type-alias. So instead of writing std::queue<int> everywere, it's just IntQueue. Same for the vector, you don't write std::vector<IntQueue> just IntQueueVector. typedefs are very useful when dealing with template classes :)
  • fredoverflow
    fredoverflow over 13 years
    Don't forget a space between the two closing angle brackets in C++98, otherwise they are interpreted as the right-shift operator ;-)
  • Yakov Galka
    Yakov Galka over 13 years
    @Moo: congratulations, you saved 7 characters in std::queue<int>, that's ~1.5 seconds of typing. Now when I look at IntQueue I don't really know what type is it, std::queue? std::deque? std::priority_queue? SomeLibrary::queue?
  • Moo-Juice
    Moo-Juice over 13 years
    @ybungalobill, irrespective of typing there are other reasons to typedef. And I'm sorry if you're still using VI to code, but my IDE tells me what the underlying type is when I mouse over ;)
  • Yakov Galka
    Yakov Galka over 13 years
    @Moo: I use MVS, but I don't use a mouse.