HELP ME!!help...C++ ABOUT QUEUE

how to make queue simple proggam..
example we input 1 2 3 4 5
and the output is 1 2 3 4 5
how to make this with pop and push??
Push user input onto a stack
Pop it off and push it onto another stack
Pop it off of that stack and print it
i mean..
i ask example script simulation in c++ language....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <queue>
#include <iostream>
int main()
{
    std::queue<int> foo;
    foo.push(1);
    foo.push(2);
    foo.push(3);
    foo.push(4);
    foo.push(5);
    while(!foo.empty()) {
        std::cout << foo.front() << " ";
        foo.pop();
    }
}
@MiiNiPaa that is incorrect, the OP asks how to do it using only pop and push.
Last edited on
Isn't this impossible?
pop returns void for me.
Well, he didn't said that he want to use stack either.
OP should tell exactly what does he want to use.
I assumed that he wants to use std::queue to store information and print it. And in standard queue (and stack BTW) pop() method returns void, so we should use front() method to read value before popping it out.

EDIT
:
Isn't this impossible?
DO THE IMPOSSIBLE!
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
#include <queue>
#include <iostream>

class Int
{
public:
    int a;
    friend std::istream& operator>>(std::istream& in, Int& x);
    Int(int x)
    {
        a = x;
    }
    ~Int()
    {
        if(a)
            std::cout << a << " ";
    }
};

std::istream& operator>>(std::istream& in, Int& x)
{
    in >> x.a;
    return in;
}

int main()
{
    Int x(0);
    std::queue<Int> foo;
    std::cin >> x;
    while(x.a != 0){
        foo.push(x);
        std::cin >> x;
    }
    while(!foo.empty()) {
        foo.pop();
    }
}
Last edited on
thx for answer...i think i only need a simple example which can we input and the output is a queue..
@minniipaa in the first script you have,i can,t compile it..-_-
Strange. It should work fine even without C++11. What error exactly do you have and on which line?
i used dev c++..cannot compile this.. no error in the line...but every i press run appear the window and suddenly lost again..
@michael9999 that is because there is no statement in the program that outputs anything - the program only asks for input. You have to write the statement to output the stuff yourself.
how to write statament to output?...i think this with output because (pop)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <queue>
#include <iostream>
int main()
{
    std::queue<int> foo;
    foo.push(1);
    foo.push(2);
    foo.push(3);
    foo.push(4);
    foo.push(5);
    while(!foo.empty()) {
        std::cout << foo.front() << " ";//This is the output (we are outputting value on top)
        foo.pop();//Delete value on top
    }
}


In second program there is, in fact, output too. But it is better not to use that approach ever.
Topic archived. No new replies allowed.