Rick_Sch
Rick_Sch

Reputation: 456

Dynamically Creating, Naming, and Placing Queues

I have a program in which a user enters in the number of queues that they want. Is it possible for my program to somehow then take that number and dynamically create queues (names and all). Also, once the queues are created, I would like to be able to loop through them, so I want to put them in an array called arrayOfQueues. The last thing to note is that the queues hold a struct called Process.

The following code might help set up my scenario better than I can type it:

UPDATED:

void function(int numQueues){
vector<queue<Process>> vectorOfQueues;
for(int i=0; i<numQueues; i++){
    vectorOfQueues.push_back(queue<Process>());
}

Upvotes: 0

Views: 510

Answers (1)

bitmask
bitmask

Reputation: 34628

If your concern is only to create an iteratable collection of queues, do as Kerrek suggests and use a vector of queues. But you say you want to name them. In that case you may want to keep them in an std::unordered_map<std::string,std::queue<Process>> in order to access them through their name.

Regarding your update, it's much too complicated. The same can be achieved easier:

std::vector<std::queue<Process>> vectorOfQueues(numQueues,std::queue<Process>{});

or

std::vector<std::queue<Process>> vectorOfQueues;
vectorOfQueues.resize(numQueues);

is sufficient.

Upvotes: 3

Related Questions