堆栈(简称为栈)是一种先入后出(First In, Last Out)的数据结构。
环境要求
Windows XP
及以上。Windows 10
、Windows 11
在Windows 功能
中勾选.NET Framework 3.5 (包括 .NET 2.0 和 3.0)
。
前置知识
(Empty = Empty)
vbBoolean, Value: True
(Null = Null)
vbNull (value unknown)
New RegExp Is New RegExp
vbBoolean, Value: False
Set oRE = New RegExp
oRE Is oRE
vbBoolean, Value: True
CreateObject("Scripting.FileSystemObject") Is CreateObject("Scripting.FileSystemObject")
vbBoolean, Value: False
Set oFS = CreateObject("Scripting.FileSystemObject")
oFS Is oFS
vbBoolean, Value: True
下面两个返回值出现的原因是浮点误差:
0.1 + 0.2 = 0.3
vbBoolean, Value: False
100000000000000000000000 = 100000000000000000000001
vbBoolean, Value: True
使用
创建一个 Stack
对象:
Set oS = CreateObject("System.Collections.Stack")
Push 方法:将元素推入栈中
Set oS = CreateObject("System.Collections.Stack")
oS.Push Empty
oS.Push Null
oS.Push "String"
oS.Push 0
oS.Push 3.14
oS.Push CreateObject("Scripting.FileSystemObject")
oS.Push New RegExp
oS.Push True
oS.Push False
Count 属性:表示当前栈内元素个数
Set oS = CreateObject("System.Collections.Stack")
oS.Count()
vbLong, Value: 0
oS.Push(666)
vbEmpty (uninitialized variable)
oS.Count()
vbLong, Value: 1
Clear 方法:清空堆栈
Set oS = CreateObject("System.Collections.Stack")
oS.Push(888)
vbEmpty (uninitialized variable)
oS.Count()
vbLong, Value: 1
oS.Clear()
vbEmpty (uninitialized variable)
oS.Count()
vbLong, Value: 0
Clone 方法:返回该堆栈的拷贝
Set oS = CreateObject("System.Collections.Stack")
oS.Push 666
Set oS2 = oS.Clone()
oS2.Push 888
oS Is oS2
vbBoolean, Value: False
oS2.Count()
vbLong, Value: 2
oS.Count()
vbLong, Value: 1
Set oS = CreateObject("System.Collections.Stack")
oS.Push 666
Set oS2 = oS
oS2.Push 888
oS Is oS2
vbBoolean, Value: True
oS2.Count()
vbLong, Value: 2
oS.Count()
vbLong, Value: 2
Set oS = CreateObject("System.Collections.Stack")
oS.Push CreateObject("Scripting.FileSystemObject")
Set oS2 = oS.Clone
oS Is oS2
vbBoolean, Value: False
oS.Peek() Is oS2.Peek()
vbBoolean, Value: True
Set oS = CreateObject("System.Collections.Stack")
oS.Push New RegExp
Set oS2 = oS.Clone
oS Is oS2
vbBoolean, Value: False
oS.Peek() Is oS2.Peek()
vbBoolean, Value: True
ToArray 方法:将堆栈转为普通 VBScript 数组
Set oS = CreateObject("System.Collections.Stack")
oS.Push 1
oS.Push 3.1415926
oS.Push True
oS.ToArray()
vbArray Len: 3
0): vbBoolean, Value: True
1): vbDouble, Value: 3.1415926
2): vbInteger, Value: 1
Set oS = CreateObject("System.Collections.Stack")
oS.Push New RegExp
oS.ToArray()(0) Is oS.Peek()
vbBoolean, Value: True
Set oS = CreateObject("System.Collections.Stack")
oS.Push CreateObject("Scripting.FileSystemObject")
oS.ToArray()(0) Is oS.Peek()
vbBoolean, Value: True
Contains 方法:检查堆栈内是否包含某元素
Set oS = CreateObject("System.Collections.Stack")
oS.Push 1
oS.Push 2
oS.Contains(0)
vbBoolean, Value: False
oS.Contains(1)
vbBoolean, Value: True
Set oS = CreateObject("System.Collections.Stack")
oS.Push Null
oS.Push oS
oS.Contains(Null)
vbBoolean, Value: True
oS.Contains(oS)
vbBoolean, Value: True
oS.Contains(CreateObject("System.Collections.Stack"))
vbBoolean, Value: False
Peek 方法:返回栈顶的元素(但不从堆栈中移除)
Set oS = CreateObject("System.Collections.Stack")
oS.Push 1
oS.Push 2
oS.Peek()
vbInteger, Value: 2
oS.Peek()
vbInteger, Value: 2
oS.ToArray()
vbArray Len: 2
0): vbInteger, Value: 2
1): vbInteger, Value: 1
Pop 方法:移除栈顶元素并将其返回
Set oS = CreateObject("System.Collections.Stack")
oS.Push 1
oS.Push 2
oS.Pop()
vbInteger, Value: 2
oS.Pop()
vbInteger, Value: 1
oS.ToArray()
vbArray Len: 0
GetHashCode 方法:返回堆栈的哈希码
Set oS = CreateObject("System.Collections.Stack")
Set oS2 = oS
Set oS3 = oS.Clone()
Set oS4 = CreateObject("System.Collections.Stack")
oS.GetHashCode()
vbLong, Value: 58225482
oS2.GetHashCode()
vbLong, Value: 58225482
oS3.GetHashCode()
vbLong, Value: 54267293
oS4.GetHashCode()
vbLong, Value: 18643596
Equals 方法:确定是否为同一个堆栈
Set oS = CreateObject("System.Collections.Stack")
Set oS2 = oS
Set oS3 = oS.Clone()
Set oS4 = CreateObject("System.Collections.Stack")
oS.Equals(oS)
vbBoolean, Value: True
oS.Equals(oS2)
vbBoolean, Value: True
oS.Equals(oS3)
vbBoolean, Value: False
oS.Equals(oS4)
vbBoolean, Value: False
ToString 方法:返回类名
Set oS = CreateObject("System.Collections.Stack")
oS.ToString()
vbString, Value: System.Collections.Stack
TypeName(oS)
vbString, Value: Stack