是否堆栈<>构造反转堆栈另一个被初始化的时候? [英] Does Stack<> constructor reverse the stack when being initialized from other one?

查看:139
本文介绍了是否堆栈<>构造反转堆栈另一个被初始化的时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是代码:

var s = new Stack<int>();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);

var ns = new Stack<int>(s);
var nss = new Stack<int>(new Stack<int>(s));



,然后让我们看看结果

and then let's see the result

        tbLog.Text += "s stack:";
        while(s.Count > 0)
        {
            tbLog.Text += s.Pop() + ",";
        }
        tbLog.Text += Environment.NewLine;
        tbLog.Text += "ns stack:";
        while (ns.Count > 0)
        {
            tbLog.Text += ns.Pop() + ",";
        }

        tbLog.Text += Environment.NewLine;
        tbLog.Text += "nss stack:";
        while (nss.Count > 0)
        {
            tbLog.Text += nss.Pop() + ",";
        }



产生下面的输出:

produces the following output:

s stack:4,3,2,1,

ns stack:1,2,3,4,

nss stack:4,3,2,1,

因此, NS 堆栈恢复取值堆栈和 NSS 堆栈是相同的取值

So, ns stack is reverted s stack and nss stack is the same as s stack.

推荐答案

堆叠构造函数需要一个的IEnumerable< T> 推动上,如果添加被多次调用

The stack constructor which takes an IEnumerable<T> pushes the items on as if Add were called multiple times.

迭代的项目。在以上弹出命令堆栈迭代......所以,当你从另一个构建一个堆栈,它将首先在原堆栈的顶部,然后把第二次从顶部元素上最重要的是在新的堆栈等..有效扭转它。

Iterating over a stack iterates in "pop" order... so when you construct one stack from another, it will add the top of the original stack first, then put the "second from the top" element on top of that in the new stack, etc... effectively reversing it.

这篇关于是否堆栈&LT;&GT;构造反转堆栈另一个被初始化的时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆