首页 > 其他分享 >【原创】在 VBScript 中使用堆栈(Stack)

【原创】在 VBScript 中使用堆栈(Stack)

时间:2023-02-07 23:13:18浏览次数:51  
标签:Set VBScript CreateObject Value Push 堆栈 oS Stack

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

环境要求

  • Windows XP 及以上。
  • Windows 10Windows 11Windows 功能 中勾选 .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

参考

标签:Set,VBScript,CreateObject,Value,Push,堆栈,oS,Stack
From: https://www.cnblogs.com/OldLiu/p/VBScript_Using_NET_Stack.html

相关文章

  • UVA12096 The SetStack Computer 栈的应用 好题
    题意翻译对于一个以集合为元素的栈,初始时栈为空。输入的命令有如下几种:PUSH:将空集{}压栈DUP:将栈顶元素复制一份压入栈中UNION:先进行两次弹栈,将获得的集合A和B取并集,将结......
  • (转)go语言-golang基础-queue队列和stack堆栈
    原文:https://www.cnblogs.com/malukang/p/12708850.html1.queue队列队列(queue),是一种FIFO(FirstInFirstOut)先进先出的线性表。通常用数据或者链表来实现队列。队......
  • 【原创】在 VBScript 中使用动态数组(ArrayList)
    环境要求WindowsXP及以上。Windows10、Windows11在Windows功能中勾选.NETFramework3.5(包括.NET2.0和3.0)。优点相比VBScript内置的数组,大小......
  • MASA Stack 1.0 发布会讲稿——趋势篇
    世界技术圈在发生什么?云原生的演进这里跟大家一起回顾一下张磊老师在云原生开发者日上关于“云原生技术演进”的分享。有很多人会问,是不是云原生就等于K8s这些开源项目......
  • 堆栈区问题
    基本数据类型所对应的引用数据类型基本数据类型中都存放在栈中,引用类型数据在堆中存放,它们的地址存在栈中Object可同一所有数据,包装类的默认值是null整数缓冲......
  • 华为FusionSphere OpenStack标签有大作用,你用对了吗?
    FusionSphere的标签有两处设置的地方,一个是主机组标签,一个是规格标签,这两个标签配置均在FusionSphereOpenStackOM上进行配置。标签与虚拟机能发放的位置、热迁移和重建的......
  • Maven - StackOverflowError
    问题与分析今天发现服务器上的Jenkins在集成项目时报错,报错原因如下:errorcompiling:java.lang.StackOverflowError->[Help1][ERROR][ERROR]Toseethefullstacktr......
  • Viewing the Call Stack in WinDbg
    ViewingtheCallStackinWinDbgThecallstackisthechainoffunctioncallsthathaveledtothecurrentlocationoftheprogramcounter.Thetopfunctiono......
  • ftrace中的Max Stack Tracer和Function_Profiler
    (1)MaxStackTracer的使用这个tracer记录内核函数的堆栈使用情况,需要使能CONFIG_STACK_TRACER,用户可以使用如下命令打开该tracer:#echo1>/proc/sys/kernel/stack......
  • npm install 报错:verbose stack Error: unable to resolve dependency tree
    错误描述>npminstallnpmWARNoldlockfilenpmWARNoldlockfileThepackage-lock.jsonfilewascreatedwithanoldversionofnpm,npmWARNoldlockfileso......