【原创】在 VBScript 中使用队列(Queue)

队列(简称为队)是一种先入先出(First In, First Out)的数据结构。

环境要求

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

前置知识

WSH.Echo Empty = Empty
-1
WSH.Echo Null = Null
null
WSH.Echo New RegExp Is New RegExp
0
Set oRE = New RegExp
WSH.Echo oRE Is oRE
-1
WSH.Echo CreateObject("Scripting.FileSystemObject") Is CreateObject("Scripting.FileSystemObject")
0
Set oFS = CreateObject("Scripting.FileSystemObject")
WSH.Echo oFS Is oFS
-1

下面两个返回值出现的原因是浮点误差:

WSH.Echo 0.1 + 0.2 = 0.3
0
WSH.Echo 100000000000000000000000 = 100000000000000000000001
-1

使用

创建一个 Queue 对象:

Set oQ = CreateObject("System.Collections.Queue")

Enqueue 方法:将元素入队

Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue Empty
oQ.Enqueue Null
oQ.Enqueue "String"
oQ.Enqueue 0
oQ.Enqueue 3.14
oQ.Enqueue CreateObject("Scripting.FileSystemObject")
oQ.Enqueue New RegExp
oQ.Enqueue True
oQ.Enqueue False

Count 属性:表示当前队内元素个数

Set oQ = CreateObject("System.Collections.Queue")
WSH.Echo oQ.Count()
0
oQ.Enqueue 666
WSH.Echo oQ.Count()
1

Clear 方法:清空队列

Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 888
WSH.Echo oQ.Count()
1
oQ.Clear
WSH.Echo oQ.Count()
0

Clone 方法:返回该队列的拷贝

Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 666
Set oQ2 = oQ.Clone()
oQ2.Enqueue 888
WSH.Echo oQ Is oQ2, oQ.Count(), oQ2.Count()
0 1 2
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 666
Set oQ2 = oQ
oQ2.Enqueue 888
WSH.Echo oQ Is oQ2, oQ.Count(), oQ2.Count()
-1 2 2
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue CreateObject("Scripting.FileSystemObject")
Set oQ2 = oQ.Clone
WSH.Echo oQ Is oQ2, oQ.Peek() Is oQ2.Peek()
0 -1
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue New RegExp
Set oQ2 = oQ.Clone
WSH.Echo oQ Is oQ2, oQ.Peek() Is oQ2.Peek()
0 -1

ToArray 方法:将队列转为普通 VBScript 数组

Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 1
oQ.Enqueue 3.1415926
oQ.Enqueue True
WSH.Echo Join(oQ.ToArray(), " ")
1 3.1415926 True
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue New RegExp
WSH.Echo oQ.ToArray()(0) Is oQ.Peek()
-1
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue CreateObject("Scripting.FileSystemObject")
WSH.Echo oQ.ToArray()(0) Is oQ.Peek()
-1

Contains 方法:检查队列内是否包含某元素

Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 1
oQ.Enqueue 2
WSH.Echo oQ.Contains(0), oQ.Contains(1)
0 -1
Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue Null
oQ.Enqueue oQ
WSH.Echo oQ.Contains(Null), oQ.Contains(oQ), oQ.Contains(CreateObject("System.Collections.Queue"))
-1 -1 0

Peek 方法:返回队首的元素(但不从队列中移除)

Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 1
oQ.Enqueue 2
WSH.Echo oQ.Peek(), oQ.Peek()
1 1
WSH.Echo Join(oQ.ToArray(), " ")
1 2

Dequeue 方法:将队首元素出队

Set oQ = CreateObject("System.Collections.Queue")
oQ.Enqueue 1
oQ.Enqueue 2
WSH.Echo oQ.Dequeue(), oQ.Dequeue(), UBound(oQ.ToArray())
1 2 -1

GetHashCode 方法:返回队列的哈希码

Set oQ = CreateObject("System.Collections.Queue")
Set oQ2 = oQ
Set oQ3 = oQ.Clone()
Set oQ4 = CreateObject("System.Collections.Queue")
WSH.Echo oQ.GetHashCode(), oQ2.GetHashCode(), oQ3.GetHashCode(), oQ4.GetHashCode()
58225482 58225482 54267293 18643596

Equals 方法:确定是否为同一个队列

Set oQ = CreateObject("System.Collections.Queue")
Set oQ2 = oQ
Set oQ3 = oQ.Clone()
Set oQ4 = CreateObject("System.Collections.Queue")
WSH.Echo oQ.Equals(oQ), oQ.Equals(oQ2), oQ.Equals(oQ3), oQ.Equals(oQ4)
-1 -1 0 0

ToString 方法:返回类名

Set oQ = CreateObject("System.Collections.Queue")
WSH.Echo oQ.ToString(), TypeName(oQ)
System.Collections.Queue Queue

参考

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老刘1号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值