vbs实现c++的vector

代码(待更新):

class Vector
    Private length
    Private data()
    Sub Class_Initialize()
        length=0
    End Sub
    '插入元素'
    public Function Add (byval x)
        length=length+1
        redim preserve data(length-1)
        data(length-1)=x
    End Function
    '快速展示'
    public Function display()
        dim i
        dim s
        for i=0 to length-1
            s=s & cstr(data(i)) & " "
        next
        msgbox s
    End Function
    '返回大小'
    public Function size()
        size= length
    End Function
    '获取元素'
    public Function getAt (byval pos)
        if pos<0 or pos>length-1 then 
            getAt=0
            exit Function
        end if
        getAt=data(pos)
    End Function
    '删除元素'
    public Function delAt (byval pos)
        if pos<0 or pos>length-1 then exit Function '注意退出函数的表达式'
        dim i
        for i=pos to length-2
            data(i)=data(i+1)
        next
        length=length-1
    End Function
    '插入元素'
    public Function insert (byval pos,byval elem)
        if pos<0 or pos>length-1 then exit Function 
        dim i
        length=length+1
        redim preserve data(length-1)
        for i=length-1 to pos+1 step -1
            data(i)=data(i-1)
        next
        data(pos)=elem
    End Function
end class

调用:

set v = new Vector
v.Add 1
v.Add 2
v.Add 3
v.display
v.delAt 0
v.insert 1,555
v.display

猜你喜欢

转载自www.cnblogs.com/TQCAI/p/8874535.html