Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1488550
  • 博文数量: 226
  • 博客积分: 3997
  • 博客等级: 少校
  • 技术积分: 2369
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-19 17:26
个人简介

Never save something for a special occasion. Every day in your life is a special occasion.

文章分类

全部博文(226)

文章存档

2018年(5)

2017年(11)

2016年(1)

2015年(17)

2014年(14)

2013年(30)

2012年(5)

2011年(52)

2010年(107)

分类: C/C++

2010-11-12 20:41:06

vb数据类型:善用VB集合数据类型

   VB集合(Collection)数据类型,可以很方便实现插入删除,工作原理类似C链表。
并且在使用了Key后检索操作非常简单;但其编程上方便却带来了效率上急剧下降(尤其在大数据量时会让你无法忍受)。以下举两个例子来讨论下怎样把 集合 和 结构 结合使用,在方便和效率间达到平衡 。


----1.要求建立数据结构用来保存学生学号姓名和成绩并在需要时以成绩高低按顺序输出这些信息。

这里我想提供两种解决思路方法
 
----第1种:完全用集合来保存数据 
 

结构

Typet MyType
    ID As Long
    Name As String
    Score As Integer
End Type

类clsData 
 
Public ID As Long
Public Name As String
Public Score As Integer

并定义函数用来接受数据并插入到数据结构中
 
Public Function InsertToCol(pData As tMyType)

'其中m_ColData保存记录
Dim myClass As New clsData
Set myClass=Nothing ' //

For iLoopCtrl=1 To m_ColData.Count
    If m_ColData(iLoopCtrl).Score <=pData.Score Then Exit For
Next

myClass.ID=pData.ID
myClass.Name=pData.Name
myClass.Score=pData.Score

If m_ColData.Count=0 Or iLoopCtrl =m_ColData.Count Then
    m_ColData.Add Item:=myClass
Else
    m_ColIndex.Add Item:=myClass, before:=iLoopCtrl  ' // before
EndIf

EndFunction 
 

这时对每个记录做处理如下
 
Public Function OutProcess
For iLoopCtrl=1 To m_ColData.Count
    CurrentID=m_ColData(iLoopCtrl).ID
    CurrentName=m_ColData(iLoopCtrl).Name
    CurrentScore=m_ColData(iLoopCtrl).Score
    '对当前记录做相应处理
Next
EndFunction


 
----第 2种: 集合和结构结合起来,结构保存数据,集合保存排序信息

定义变量
Public m_Array(99) As tMyType   '根据需要也可以定义成动态 
Public m_ColIndex As New Collection   '用来保存索引信息

插入数据 

Public Function InsertToArray(pData As tMyType)
If iCurIndex>99 Then Exit Function
For iLoopCtrl=1 To m_ColIndex.Count
If m_Array(m_ColIndex(iLoopCtrl)).Score <=pData.Score Then Exit For
Next
If m_ColIndex.Count=0 Or iLoopCtrl =m_ColIndex.Count Then
m_ColIndex.Add iLoopCtrl-1
Else
m_ColIndex.Add iLoopCtrl-1,before:=iLoopCtrl
EndIf
m_Array(iCurIndex).ID=pData.ID
m_Array(iCurIndex).Name=pData.Name
m_Array(iCurIndex).Score=pData.Score
iCurIndex=iCurIndex1
End Function


这时对每个记录做处理如下
 
Public Function OutProcess
For iLoopCtrl=1 To m_ColData.Count
I=m_ColData(iLoopCtrl)
CurrentID=m_Array(I).ID
CurrentName=m_Array(I).Name
CurrentScore=m_Array(I).Score
'对当前记录做相应处理
Next
End Function

*性能分析
对于集合来讲 随着记录个数增长操作效率飞快下降,因为集合按下标查找记录时需要进行遍历。

集合+数组的方式 结合了 数组的随机访问 和 集合的高效增删 的优点。


----2.当记录有唯关键字并经常以这个关键字做查询时可以使用以下思路方法

定义用于保存数据结构和结构
 
Type tMyType
    Item_1 As String '为关键字
    Item_2 As String
    Item_3 As String
End Type

Public m_Array As tMyType
Public m_ColIndex As New Collection   '用于保存索引集合
 
定义用于保存索引信息类clsIndex如下
 
Public Item_Key As String
Public ID_OfArray As Integer

插入记录pData的过程如下
 
Public Function InsertData(pData As tMyType)
Dim myClass As New clsIndex
ID_OfArray=ID_OfArray1 ' // ?Get a free index of m_Array?
m_Array(ID_OfArray).Item_1=pData.Item_1
m_Array(ID_OfArray).Item_2=pData.Item_2
m_Array(ID_OfArray).Item_3=pData.Item_3
myClass.Item_Key=pData.Item_1
myClass.ID_OfArray=ID_OfArray
m_ColIndex.Add Item:=myClass,Key:=pData.Item_1
End Function

以给出关键字(mKey) 取得数据
 
Current_Item1=m_Array(myClass(mKey) .ID_OfArray).Item_1
Current_Item2=m_Array(myClass(mKey) .ID_OfArray).Item_2
Current_Item3=m_Array(myClass(mKey) .ID_OfArray).Item_3

 

阅读(3382) | 评论(0) | 转发(0) |
0

上一篇:VB6鼠标滚轮插件

下一篇:gawk 手册-simplify

给主人留下些什么吧!~~