在C++使用Vector动态保持结构数组信息

  比如我们要取系统的日志信息,如果先获取到日志个数然后再分配空间的话,时间会比较长,影响效率。如果使用Vector动态添加的话,问题就解决了。在这个例子里,自定义了一个类,在类中使用双重的Vector来保存信息。


/*----------------------------------------------------------------
// Copyright (C) 2004 浪潮(北京)电子信息有限公司
// 版权所有。 
//
// 文件名:bufArrayList.h
// 文件功能描述:这个类用来保存从动态连接库中显示的信息,采用了多层Vector的形式。
//               先保存每一个实例的属性和值到一个Vector中,然后在保存其实例到另外
//               一个Vector列表中。
//
// 作者:刘正伟
// 创建标识:2005/8/24
//
//----------------------------------------------------------------
*/

#pragma once

#include 
< vector >

#ifndef BUFFARRAYDLL 

#define  BUFFARRAYDLLEXTEN __declspec(dllimport) 
#else  
#define  BUFFARRAYDLLEXTEN __declspec(dllexport) 
#endif  


using   namespace  std ;

// coputerInfo
typedef  struct  BUFFARRAYDLLEXTEN _PerportyVauleMAP  // Win32_ComputerSystem
{
    
//属性名称
    char    key[256]  ;
    
//属性名称对应的值
    char    vaule[256];
}
PerportyVauleMAP;


// 定义一个Vector用来保存每一个实例的所有属性信息,里面保存的是一个个PerportyVauleMAP结构数组
typedef vector < PerportyVauleMAP >  INSTANCEVECTOR;
// 定义了一个Vector用来保存所有的实例列表,里面保存的是INSTANCEVECTOR类型
typedef vector < INSTANCEVECTOR >  INSTANCELISTVECTOR;

 
class  BUFFARRAYDLLEXTEN CBufArrayList
{
public:
    CBufArrayList(
void);
    
~CBufArrayList(void);
private:
    
//STRINFGVECTOR strParamName;
    INSTANCELISTVECTOR _instanceListVt;
    
//set the property's value to the vector 

public:

    
/*
    * 函数介绍 : 添加一个实例的某一个属性名称和属性值;
    * 输入参数 : int vtLocation,Vector 的索引号,string name 属性名称, string value, 要赋予的值;
    * 输出参数 : null;
    * 返回值 : INSTANCEVECTOR类型, 返回加入之后保存实例的Vector.SetPerportyAndVaule
    
*/

   
void SetPerportyAndVaule(int vtLocation,char* name,char* value);
   
/*
   * 函数介绍 : 将一个实例添加到实例列表中;
   * 输入参数 : int& vtLocation
   * 输出参数 : vtLocation;
   * 返回值 : void.AddNewInstance
   
*/

   
void AddNewInstance(int& vtLocation);
   
/*
   * 函数介绍 : 清除一个实例列表中保存的属性和值信息
   * 输入参数 : null;
   * 输出参数 : null;
   * 返回值 : void.
   
*/

  
   
void ClearAll();
 
   
/*
   * 函数介绍 : 根据实例和属性的位置,枚举出属性的名称和值
   * 输入参数 : int instanceNumber,实例所在的位置int propertyNumber,属性所在的位置,PerportyVauleMAP &hash;属性结构
   * 输出参数 : PerportyVauleMAP;
   * 返回值 : bool 是否成功.
   
*/

   
bool GetPropertyValue(int instanceNumber,int propertyNumber,PerportyVauleMAP &hash);
   
/*
   * 函数介绍 : 返回实例个数
   * 输入参数 : null;
   * 输出参数 : null;
   * 返回值 : 实例个数
   
*/

   
int GetInstanceListSize();
   
/*
   * 函数介绍 : 返回属性个数
   * 输入参数 : null;
   * 输出参数 : null;
   * 返回值 : 属性个数
   
*/

   
int GetPropertySize();

}
;


CPP文件:

#include  " StdAfx.h "

#define  BUFFARRAYDLL

#include 
" ./bufarraylist.h "
CBufArrayList::CBufArrayList(
void )
{

}


CBufArrayList::
~ CBufArrayList( void )
{
    _instanceListVt.clear();
}


/*
* 函数介绍 : 添加一个实例的某一个属性名称和属性值;
* 输入参数 : string name 属性名称, string value, 要赋予的值;
* 输出参数 : null;
* 返回值 : INSTANCEVECTOR类型, 返回加入之后保存实例的Vector.
*/

void  CBufArrayList::SetPerportyAndVaule( int  vtLocation, char *  name, char *  value)
{
    PerportyVauleMAP pmap ;
    strcpy(pmap.key,name);
    strcpy(pmap.vaule, value);
    (_instanceListVt[vtLocation]).push_back(pmap);

}

/*
* 函数介绍 : 将一个实例添加到实例列表中;
* 输入参数 : INSTANCEVECTOR vt 保存一个实例信息的Vector;
* 输出参数 : null;
* 返回值 : void.
*/

void  CBufArrayList::AddNewInstance( int &  vtLocation)
{
    INSTANCEVECTOR vt;
    _instanceListVt.push_back(vt);
    vtLocation 
= _instanceListVt.size() -1;
}



/*
* 函数介绍 : 清除一个实例列表中保存的所有实例信息,也清除实例Vector信息
* 输入参数 : null;
* 输出参数 : null;
* 返回值 : void.
*/

void  CBufArrayList::ClearAll()
{
    _instanceListVt.clear();
}

/*
* 函数介绍 : 返回属性个数
* 输入参数 : null;
* 输出参数 : null;
* 返回值 : 属性个数
*/

int  CBufArrayList::GetPropertySize()
{
    
if (!_instanceListVt.empty())
    
{

        INSTANCEVECTOR vt 
= (INSTANCEVECTOR) _instanceListVt[0];
        
if (vt.empty())
        
{
            
return 0;
        }

        
else
        
{
            
return vt.size();
        }

    }

    
else
    
{
        
return 0;
    }

}

/*
* 函数介绍 : 返回实例个数
* 输入参数 : null;
* 输出参数 : null;
* 返回值 : 实例个数
*/

int  CBufArrayList::GetInstanceListSize()
{
    
int count = 0;
    
if (_instanceListVt.empty())
    
{
        
return 0;
    }

    
else
    
{
        count 
= _instanceListVt.size();
        
return count;
    }

}

/*
* 函数介绍 : 根据实例和属性的位置,枚举出属性的名称和值
* 输入参数 : int instanceNumber,实例所在的位置int propertyNumber,属性所在的位置,PerportyVauleMAP &hash;属性结构
* 输出参数 : PerportyVauleMAP;
* 返回值 : bool 是否成功.
*/

 
bool  CBufArrayList::GetPropertyValue( int  instanceNumber, int  propertyNumber,PerportyVauleMAP  & hash)
{
    
if (!_instanceListVt.empty())
    
{

        INSTANCEVECTOR vt 
= (INSTANCEVECTOR) _instanceListVt[instanceNumber];
        
if (vt.empty())
        
{
            
return false;
        }

        
else
        
{
            PerportyVauleMAP p 
= (PerportyVauleMAP)vt[propertyNumber];
            strcpy(hash.key , p.key);
            strcpy(hash.vaule, p.vaule);
        }

    }

    
else
    
{
        
return false;
    }


    
return true;
}




使用:

#include  " stdafx.h "
#include 
" BufArrayList.h "
int  _tmain( int  argc, _TCHAR *  argv[])
{
    CBufArrayList buf;
    
int index = 0;
    
for(int i=0;i<20000;i++)
    
{
            buf.AddNewInstance(index);
            printf(
"index:%d",index);
            buf.SetPerportyAndVaule(index,
"name","sundy");
            buf.SetPerportyAndVaule(index,
"age","123");
    }


    getchar();
    
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值