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

[原创] 在 VBScript 中使用队列(Queue)

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

队列(简称为队)是一种先入先出(First In, First 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
复制代码
使用

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

嗯 布尔值能不能truefalse写 0 -1不直观
难道wsh.echo会把布尔转化为0 -1?

TOP

回复 2# jyswjjgdwtdtj


    嗯,用的 jupyter 和一个我魔改的 iVBScript jupyter kernel,那个输出是自动生成的。
这篇文的工作流是从 ipynb 到 md  再到 bbcode

TOP

本帖最后由 jyswjjgdwtdtj 于 2023-7-24 10:28 编辑

回复 3# 老刘1号


    嗯 所以说0是false 1是true吗

TOP

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

回复 4# jyswjjgdwtdtj
  1. If 0 = False Then WSH.Echo "0 = False"
  2. If -1 = True Then WSH.Echo "-1 = True"
复制代码
  1. WSH.Echo True, CLng(True), CInt(True), CByte(True)
  2. WSH.Echo False, CLng(False), CInt(False), CByte(False)
复制代码
手动测试,请

TOP

回复 4# jyswjjgdwtdtj


    我猜想可能和 VBS 的隐式类型转换有关系

TOP

回复 6# 老刘1号


    嗯 -1的确是true 且cint(true)也的确是-1
感觉一般代表true的都是1啊

TOP

回复 7# jyswjjgdwtdtj
  1. if 0 then msgbox 0
  2. if 1 then msgbox 1
  3. if -1 then msgbox -1
复制代码
可以测得vbs中也是遵循非0为真0为假的
不过官方把true的值定为-1,我感觉是另一个极端,因为-1的补码表示所有二进制位都是1,“真”的不能再“真”了(

TOP

返回列表