如何打印std::stack的内容并返回它的大小?

时间:2022-04-19 07:35:21

In c++ how can I print out the contents of my stack and return its size?

在c++中,如何打印堆栈的内容并返回其大小?

std::stack<int>  values;
values.push(1);
values.push(2);
values.push(3);

// How do I print the stack?

4 个解决方案

#1


16  

You could make a copy of the stack and pop items one-by-one to dump them:

您可以复制堆栈和弹出项逐一转储它们:

#include <iostream>
#include <stack>
#include <string>

int main(int argc, const char *argv[])
{
    std::stack<int> stack;
    stack.push(1); 
    stack.push(3); 
    stack.push(7); 
    stack.push(19); 

    for (std::stack<int> dump = stack; !dump.empty(); dump.pop())
        std::cout << dump.top() << '\n';

    std::cout << "(" << stack.size() << " elements)\n";

    return 0;
}

Output

输出

19
7
3
1
(4 elements)

See it live here: http://liveworkspace.org/code/9489ee305e1f55ca18c0e5b6fa9b546f

#2


3  

The only way to print the elements of a std::stack without popping them, is to write an adapter that extends std::stack (here's an example). Otherwise, you should replace your stack with a std::deque.

打印std::stack的元素而不弹出它们的唯一方法是编写一个扩展std::stack的适配器(这里有一个例子)。否则,您应该用std::deque替换您的堆栈。

#3


2  

Both std::stack and std::queue are wrappers around a general container. That container is accessible as the protected member c. Using c you can gain efficient access to the elements; otherwise, you can just copy the stack or queue and destructively access the elements of the copy.

std::stack和std::queue都是一个通用容器的包装器。该容器可作为受保护成员c访问。使用c可以有效地访问元素;否则,您可以复制堆栈或队列,并对复制的元素进行析构访问。

Example of using c:

使用c的例子:

#include <iostream>     // std::wcout, std::endl
#include <stack>        // std::stack
#include <stddef.h>     // ptrdiff_t
using namespace std;

typedef ptrdiff_t   Size;
typedef Size        Index;

template< class Elem >
Size nElements( stack< Elem > const& c )
{
    return c.size();
}

void display( stack<int> const& numbers )
{
    struct Hack
        : public stack<int>
    {
        static int item( Index const i, stack<int> const& numbers )
        {
            return (numbers.*&Hack::c)[i];
        }
    };

    wcout << numbers.size() << " numbers." << endl;
    for( Index i = 0;  i < nElements( numbers );  ++i )
    {
        wcout << "  " << Hack::item( i, numbers ) << endl;
    }
}

int main()
{
    stack<int>  numbers;
    for( int i = 1;  i <= 5;  ++i ) { numbers.push( 100*i ); }

    display( numbers );
}

#4


0  

http://www.cplusplus.com/reference/stl/stack/ for the size it's easy use :

http://www.cplusplus.com/reference/stl/stack/对于它的大小很容易使用:

cout << mystack.size();

For the rest i didn't see anything about in the doc but you should print the content of your stack when you push it, or have a list with it to keep a record of the element just in order to print it, don't forget to delete it when you're done testing

其余的我没有看到任何关于医生但你应该打印栈,当你把它的内容,或者有一个列表元素的记录只是为了打印出来,别忘了删除它当你完成测试

#1


16  

You could make a copy of the stack and pop items one-by-one to dump them:

您可以复制堆栈和弹出项逐一转储它们:

#include <iostream>
#include <stack>
#include <string>

int main(int argc, const char *argv[])
{
    std::stack<int> stack;
    stack.push(1); 
    stack.push(3); 
    stack.push(7); 
    stack.push(19); 

    for (std::stack<int> dump = stack; !dump.empty(); dump.pop())
        std::cout << dump.top() << '\n';

    std::cout << "(" << stack.size() << " elements)\n";

    return 0;
}

Output

输出

19
7
3
1
(4 elements)

See it live here: http://liveworkspace.org/code/9489ee305e1f55ca18c0e5b6fa9b546f

#2


3  

The only way to print the elements of a std::stack without popping them, is to write an adapter that extends std::stack (here's an example). Otherwise, you should replace your stack with a std::deque.

打印std::stack的元素而不弹出它们的唯一方法是编写一个扩展std::stack的适配器(这里有一个例子)。否则,您应该用std::deque替换您的堆栈。

#3


2  

Both std::stack and std::queue are wrappers around a general container. That container is accessible as the protected member c. Using c you can gain efficient access to the elements; otherwise, you can just copy the stack or queue and destructively access the elements of the copy.

std::stack和std::queue都是一个通用容器的包装器。该容器可作为受保护成员c访问。使用c可以有效地访问元素;否则,您可以复制堆栈或队列,并对复制的元素进行析构访问。

Example of using c:

使用c的例子:

#include <iostream>     // std::wcout, std::endl
#include <stack>        // std::stack
#include <stddef.h>     // ptrdiff_t
using namespace std;

typedef ptrdiff_t   Size;
typedef Size        Index;

template< class Elem >
Size nElements( stack< Elem > const& c )
{
    return c.size();
}

void display( stack<int> const& numbers )
{
    struct Hack
        : public stack<int>
    {
        static int item( Index const i, stack<int> const& numbers )
        {
            return (numbers.*&Hack::c)[i];
        }
    };

    wcout << numbers.size() << " numbers." << endl;
    for( Index i = 0;  i < nElements( numbers );  ++i )
    {
        wcout << "  " << Hack::item( i, numbers ) << endl;
    }
}

int main()
{
    stack<int>  numbers;
    for( int i = 1;  i <= 5;  ++i ) { numbers.push( 100*i ); }

    display( numbers );
}

#4


0  

http://www.cplusplus.com/reference/stl/stack/ for the size it's easy use :

http://www.cplusplus.com/reference/stl/stack/对于它的大小很容易使用:

cout << mystack.size();

For the rest i didn't see anything about in the doc but you should print the content of your stack when you push it, or have a list with it to keep a record of the element just in order to print it, don't forget to delete it when you're done testing

其余的我没有看到任何关于医生但你应该打印栈,当你把它的内容,或者有一个列表元素的记录只是为了打印出来,别忘了删除它当你完成测试