954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to allocate x number of queues?

I'm trying to write a a program that simulates a multi queues, multi server such as a grocery line at the grocery store.

The user will enter the desired number of queue/server pair.

how would i write this? I mean, I can have 3 - 4 different queue stacks as default, but I'm not sure how to do it if the user actually chooses the number of queues/stacks.

Any help would be greatly appreciated!

here is the link to the project if my english suck.
http://web.stcloudstate.edu/bajulstrom/cs301/projects/p6s09.html

homeryansta
Junior Poster in Training
87 posts since Jan 2009
Reputation Points: 46
Solved Threads: 1
 

Try something like this:

typedef int         Data;
typedef queue<Data> Queue;

vector<Queue> v;
for (int i = 0; i < nQueues; ++i)
    v.push_back (Queue());

for (int i = 0; i < nQueues; ++i)
    for (int j = 0; j < 5; ++j) // some data
        v[i].push (j);
nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 

Try something like this:

typedef int         Data;
typedef queue<Data> Queue;

vector<Queue> v;
for (int i = 0; i < nQueues; ++i)
    v.push_back (Queue());

for (int i = 0; i < nQueues; ++i)
    for (int j = 0; j < 5; ++j) // some data
        v[i].push (j);


I don't understand templates very well, but what I'm getting from this is:

for (int i = 0; i < nQueues; ++i)
    v.push_back (Queue());

allocates each queue

for (int i = 0; i < nQueues; ++i)
    for (int j = 0; j < 5; ++j) // some data
        v[i].push (j);

throws junk into each queue right?

homeryansta
Junior Poster in Training
87 posts since Jan 2009
Reputation Points: 46
Solved Threads: 1
 

Yes. I threw some ints into it to show how to access it, but you can make Data whatever you need.

nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 
Yes. I threw some ints into it to show how to access it, but you can make Data whatever you need.

thanks, i'll read up more on vectors since I don't understand it. If i run into any more trouble, you'll see me back here :).

homeryansta
Junior Poster in Training
87 posts since Jan 2009
Reputation Points: 46
Solved Threads: 1
 

ok, back with more questions.

what is push_back?

I want to say it is a function, but not certain.

homeryansta
Junior Poster in Training
87 posts since Jan 2009
Reputation Points: 46
Solved Threads: 1
 

ok, back with more questions.

what is push_back?

I want to say it is a function, but not certain.

You need to become comfortable with checking documentation. It's a big part of programming. Find a C++ site that you like. I like http://cplusplus.com . Just type in what you need into the site's search engine.

http://www.cplusplus.com/query/search.cgi?q=vector+push_back

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

thanks everyone, I didn't end up using push_back, but I sure did use vectors!! strange thing is that I never learned vectors in my intro to computer science class. I wonder why? Anyone else have the same experience?

homeryansta
Junior Poster in Training
87 posts since Jan 2009
Reputation Points: 46
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You