last queue question

i feel bad about bugging everyone with my stupid queue assignment but i have one last question. i have to show the entire queue list and i don't know how to get the program to show that. on this site it shows how to tell the program to show the first in queue and last in the queue but i need ALL in queue. does anyone know how this can be done? my problem starts at about line 43 with if(iChoice == 3). here's what i have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <list>
#include <queue>
#include <string>
using namespace std;

int main()
{
    queue<string> myqueue;
    string strName;
    char cContinue;
    char cAccess;
    int iChoice;


    while(1)
    {
        cout << "Hello! Welcome to Dr.Smith's office! \n" << endl;
        cout << "To continue, press any key" << endl;
        cin >> cContinue;
        cout << "If you would like to put your name on the waiting list, please enter '1'" << endl;
        cout << "If you would like to see who is next in line, please enter '2'" << endl;
        cout << "If you would like to access the entire queue, please enter '3'" << endl;
        cin >> iChoice;
        if(iChoice == 1)
        {
            cout << "Please enter your name" << endl;
            cin >> strName;
            cout << "Thank you " << strName << "! Please be seated and the doctor will see you momentarily." << endl;
            myqueue.push(strName);
        }
        if(iChoice == 2)
        {
            cout << "The Next person in line is ";
            cout << "" << myqueue.front() << endl;
            myqueue.pop();
            if(myqueue.empty())
            {
                cout << "There is no one in the queue." << endl;
            }
        }
        if(iChoice == 3)
        {
            for(int x=0; x < myqueue.size();x--)
            {

                cout << "These are the people currently waiting: \n" << endl;

                cout << "" <<  << endl;
            }
            if(myqueue.empty())
            {
                cout << "There is no one in the queue." << endl;
            }
        }


    }
return 0;
It seems that you can't..
queue is a wrapper for other stl containers, it has less functionality to make it easier to use, I guess.
Just use deque instead. You'll have to change push to push_back and pop to pop_front. You'll then be able to access elements with a [] operator of using iterators.
Here's one way that should work:

Create a temporary queue (tmp).

Loop through the members of myqueue, pop the members off , display them, and add them to tmp.

swap(myqueue, tmp);
yeah, but that's a bit inefficient..
Why not have a temp var of the type std::string and set it the the member at front, then pop him off, std::cout the temp string, then push him back on line. repeat for all members:
1
2
3
4
5
6
7
8
9
10
11
12
13
void who_is_on_line (std::queue<std::string> &line) // You could just pas a copy, but I just did it by reference. 
// Cannot be const with this method.
{
  std:string temp;
  for(int i = 0; i < line.size(); ++i)
  {
    temp = line.front();
    line.pop();
    std::cout << temp << std::endl;
    line.push(temp);
  }

}
Last edited on
it seems, this application is misusing queue data structure as it requires to access the elements from middle. stl queues are only efficient when you are playing with start and end. if you want to access the elements from middle, the efficient data structure would be arrays or vectors.
hamsterman wrote:
yeah, but that's a bit inefficient.


Your point is well taken.

An efficient answer using std::queue is to explicitly construct the queue with a container.

1
2
std::deque<std::string> mycontainer;  // As hamsterman suggested.
std::queue<std::string> myqueue(mycontainer);


Do all the queue-like operations with myqueue, and when you need to access all the elements, use mycontainer.
@PanGalactic

I also read that portion of the documentation intensely, but unfortunately the base container is just copied. The modifications to the queue will not be reflected back. Now, why, why didn't they make use of the passed container directly in the adaptor is beyond me.
Crap, you are right. I thought it was a true adapter.
Topic archived. No new replies allowed.