c ++ - 打印队列中的所有节点

时间:2014-05-22 13:26:53

标签: c++

如何打印队列中的所有节点?

我试图这样做,但它回复了我的地址指针。

    #include <queue>
    struct node
    {
        int weight;
        int value;
        float bound;
    }  

    std::queue<node> q;
    node n;

    q.push(n);
    while(!q.empty())
    {
        n = q.front();
        std::cout << &n << std::endl;
        q.pop();
    }

2 个答案:

答案 0 :(得分:5)

正在打印地址,使用&#34;地址&#34;操作者:

std::cout << &n << std::endl;

我怀疑你插入了&#34;&amp;&#34;阻止编译器抱怨<<没有node运算符 提供一个更有效:

std::ostream& operator<< (std::ostream& os, const node& n)
{
     // Output the data from 'n' on 'os' the way you like...
     os << "whatever";
     return os;
}

答案 1 :(得分:-1)

std::cout << n.member_variables << std::endl;

这个怎么样?