[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创] 在 VBScript 中使用堆栈(Stack)

本帖最后由 老刘1号 于 2023-7-22 14:24 编辑

堆栈(简称为栈)是一种先入后出(First In, Last Out)的数据结构。

环境要求


  • Windows XP 及以上。
  • Windows 10Windows 11Windows 功能 中勾选 .NET Framework 3.5 (包括 .NET 2.0 和 3.0)

前置知识
  1. WSH.Echo Empty = Empty
复制代码
  1. -1
复制代码
  1. WSH.Echo Null = Null
复制代码
  1. null
复制代码
  1. WSH.Echo New RegExp Is New RegExp
复制代码
  1. 0
复制代码
  1. Set oRE = New RegExp
  2. WSH.Echo oRE Is oRE
复制代码
  1. -1
复制代码
  1. WSH.Echo CreateObject("Scripting.FileSystemObject") Is CreateObject("Scripting.FileSystemObject")
复制代码
  1. 0
复制代码
  1. Set oFS = CreateObject("Scripting.FileSystemObject")
  2. WSH.Echo oFS Is oFS
复制代码
  1. -1
复制代码
下面两个返回值出现的原因是浮点误差:
  1. WSH.Echo 0.1 + 0.2 = 0.3
复制代码
  1. 0
复制代码
  1. WSH.Echo 100000000000000000000000 = 100000000000000000000001
复制代码
  1. -1
复制代码
使用

创建一个 Stack 对象:
  1. Set oS = CreateObject("System.Collections.Stack")
复制代码
Push 方法:将元素推入栈中
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push Empty
  3. oS.Push Null
  4. oS.Push "String"
  5. oS.Push 0
  6. oS.Push 3.14
  7. oS.Push CreateObject("Scripting.FileSystemObject")
  8. oS.Push New RegExp
  9. oS.Push True
  10. oS.Push False
复制代码
Count 属性:表示当前栈内元素个数
  1. Set oS = CreateObject("System.Collections.Stack")
  2. WSH.Echo oS.Count()
复制代码
  1. 0
复制代码
  1. oS.Push 666
  2. WSH.Echo oS.Count()
复制代码
  1. 1
复制代码
Clear 方法:清空堆栈
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push 888
  3. WSH.Echo oS.Count()
复制代码
  1. 1
复制代码
  1. oS.Clear
  2. WSH.Echo oS.Count()
复制代码
  1. 0
复制代码
Clone 方法:返回该堆栈的拷贝
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push 666
  3. Set oS2 = oS.Clone()
  4. oS2.Push 888
  5. WSH.Echo oS Is oS2, oS.Count(), oS2.Count()
复制代码
  1. 0 1 2
复制代码
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push 666
  3. Set oS2 = oS
  4. oS2.Push 888
  5. WSH.Echo oS Is oS2, oS.Count(), oS2.Count()
复制代码
  1. -1 2 2
复制代码
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push CreateObject("Scripting.FileSystemObject")
  3. Set oS2 = oS.Clone
  4. WSH.Echo oS Is oS2, oS.Peek() Is oS2.Peek()
复制代码
  1. 0 -1
复制代码
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push New RegExp
  3. Set oS2 = oS.Clone
  4. WSH.Echo oS Is oS2, oS.Peek() Is oS2.Peek()
复制代码
  1. 0 -1
复制代码
ToArray 方法:将堆栈转为普通 VBScript 数组
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push 1
  3. oS.Push 3.1415926
  4. oS.Push True
  5. WSH.Echo Join(oS.ToArray(), " ")
复制代码
  1. True 3.1415926 1
复制代码
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push New RegExp
  3. WSH.Echo oS.ToArray()(0) Is oS.Peek()
复制代码
  1. -1
复制代码
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push CreateObject("Scripting.FileSystemObject")
  3. WSH.Echo oS.ToArray()(0) Is oS.Peek()
复制代码
  1. -1
复制代码
Contains 方法:检查堆栈内是否包含某元素
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push 1
  3. oS.Push 2
  4. WSH.Echo oS.Contains(0), oS.Contains(1)
复制代码
  1. 0 -1
复制代码
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push Null
  3. oS.Push oS
  4. WSH.Echo oS.Contains(Null), oS.Contains(oS), oS.Contains(CreateObject("System.Collections.Stack"))
复制代码
  1. -1 -1 0
复制代码
Peek 方法:返回栈顶的元素(但不从堆栈中移除)
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push 1
  3. oS.Push 2
  4. WSH.Echo oS.Peek(), oS.Peek()
复制代码
  1. 2 2
复制代码
  1. WSH.Echo Join(oS.ToArray(), " ")
复制代码
  1. 2 1
复制代码
Pop 方法:移除栈顶元素并将其返回
  1. Set oS = CreateObject("System.Collections.Stack")
  2. oS.Push 1
  3. oS.Push 2
  4. WSH.Echo oS.Pop(), oS.Pop(), UBound(oS.ToArray())
复制代码
  1. 2 1 -1
复制代码
GetHashCode 方法:返回堆栈的哈希码
  1. Set oS = CreateObject("System.Collections.Stack")
  2. Set oS2 = oS
  3. Set oS3 = oS.Clone()
  4. Set oS4 = CreateObject("System.Collections.Stack")
  5. WSH.Echo oS.GetHashCode(), oS2.GetHashCode(), oS3.GetHashCode(), oS4.GetHashCode()
复制代码
  1. 58225482 58225482 54267293 18643596
复制代码
Equals 方法:确定是否为同一个堆栈
  1. Set oS = CreateObject("System.Collections.Stack")
  2. Set oS2 = oS
  3. Set oS3 = oS.Clone()
  4. Set oS4 = CreateObject("System.Collections.Stack")
  5. WSH.Echo oS.Equals(oS), oS.Equals(oS2), oS.Equals(oS3), oS.Equals(oS4)
复制代码
  1. -1 -1 0 0
复制代码
ToString 方法:返回类名
  1. Set oS = CreateObject("System.Collections.Stack")
  2. WSH.Echo oS.ToString(), TypeName(oS)
复制代码
  1. System.Collections.Stack Stack
复制代码
参考

返回列表