什么现实世界中使用&QUOT的;栈"对象(.NET)你用 [英] What real world uses of the "Stack" object (.Net) have you used

查看:137
本文介绍了什么现实世界中使用&QUOT的;栈"对象(.NET)你用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都读到或听到的Stack类,但是很多人可能都没有找到一个理由来使用后进先出的对象。我好奇地听到了使用这个对象,为什么现实世界的解决方案。

We have all read about or heard about the stack class, but many of us have probably never found a reason to use the LIFO object. I am curious to hear of real world solutions that used this object and why.

http://msdn.microsoft.com/en-美国/库/ system.collections.stack.aspx

我最近看到一个例子,其中一个程序员使用的堆栈来保持自己的当前位置的轨道,同时遍历分层数据源。当他向下移动的层次,他把他的位置识别到堆栈中并作为他回到了他弹出堆栈的项目。我认为这是一个非常efficent的方式来跟踪自己的当前位置在mamoth层次。我以前从来没有见过这个。

I recently saw an example where a programmer used a stack to keep track of his current position while traversing a hierarchical data source. As he moved down the hierarchy, he pushed his position identifier on to the stack and as he moved back up he popped items off the stack. I thought this was a very efficent way to keep track of his current position in a mamoth hierarchy. I had never seen this before.

任何人有任何的例子?

推荐答案

我用它们来跟踪撤消和重做动作。

I've used them to keep track of Undo and Redo actions.

我用一个界面是这样的:

I use an interface something like this:

interface ICommand
{
    void Execute();
    void Undo();
    string Description { get; }
}

撤销和重做类型都是堆栈< ICommand的> 。然后,我创建一个具体的类一个给定的动作。在类的构造函数,我传递任何信息,我需要扶住。 执行做的动作开始,而且重做它; 撤消解开它,效果显着。它的工作原理是这样的:

Undo and Redo are both of type Stack<ICommand>. Then I create a concrete class for a given action. In the class's constructor, I pass in any information I'd need to hold on to. Execute does the action initially, and also redoes it; Undo undoes it, obviously. It works like this:

  • 在撤消一个动作:流行撤消栈,并添加到重做堆栈
  • 在重做撤销的操作:弹出重做栈,并添加到撤消再次叠加
  • 在执行一个新动作:添加到撤消堆栈和清除重做堆栈(因为国家不再一致)

我发现,你必须要小心,你真的撤销这事。例如,假设你有两个列表框的UI,每个有五个项目在里面。你的行动可能是点击一个按钮,将一切都从左边的列表到右侧列表(所以现在有十个,而左侧列表具有零)。

I found that you have to take care that you're really undoing what was done. For instance, say you have a UI with two listboxes, and each has five items in it. Your action might be to click a button to move everything on the left list to the right list (so it now has ten, and the left list has zero).

撤消动作的没有的移动一切恢复;撤消操作是回迁只有五个,你居然感动,并留下了别人。

The undo action is not to move everything back; the undo action is to move back only the five you actually moved, and leave the others.

这篇关于什么现实世界中使用&QUOT的;栈&QUOT;对象(.NET)你用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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