C/C++ 语言兼容的动态数组
来源:互联网 发布:excel去除重复数据 编辑:程序博客网 时间:2024/05/19 18:48
CSDN不厚道,竟然把我粘贴的代码改了,本来代码里面是是\,都改成了/,
/************************************************************************//* filename : vect.h *//* author : lichiyang#eyou.com *//* description : C/C++ compatible dynamic vector *//* version : 1.0 *//* date : 2011.4.2 *//************************************************************************/#ifndef __C_VECTOR_H__#define __C_VECTOR_H__#define DECLARE_VECTOR(type) /struct _Vec##type /{/void (*pInit)(struct _Vec##type *, int);/type * (*pAlloc)(int);/void (*pFree)(type *);/int (*pAdd)(struct _Vec##type *, type);/int (*pDel)(struct _Vec##type *, int);/int (*pDelAll)(struct _Vec##type *, type);/int (*pHas)(struct _Vec##type *, type);/int (*pEnsureCap)(struct _Vec##type *, int);/int nCount;/int nCaption;/type * pData;/};/typedef struct _Vec##type VECTOR_##type, *PVECTOR_##type#define IMPLEMENT_VECTOR(type) /type * _Vec##type##Alloc(int n)/{/return (type *)malloc(n * sizeof(type));/}/void _Vec##type##Free(type * p)/{/free((void *)p);/}/int _Vec##type##EnsureCap(struct _Vec##type * pVec, int cap)/{/if(cap > pVec->nCaption)/{/int i;/type *pNewData = 0;/int newCap = pVec->nCaption * 2;/if (newCap == 0)/newCap = 8;/while(newCap < cap)/newCap *= 2;/pNewData = pVec->pAlloc(newCap);/if (pNewData == 0) return 0;/for (i=0; i<pVec->nCount; i++)/pNewData[i] = pVec->pData[i];/pVec->pFree(pVec->pData);/pVec->pData = pNewData;/pVec->nCaption = newCap;/}/return 1;/}/int _Vec##type##Add(struct _Vec##type * pVec, type e)/{/if(!pVec->pEnsureCap(pVec, pVec->nCount + 1)) return 0;/pVec->pData[pVec->nCount ++] = e;/return 1;/}/int _Vec##type##Del(struct _Vec##type * pVec, int nIndex)/{/int i;/int nCount;/if (nIndex >= pVec->nCount)/return 0;/pVec->nCount--;/nCount = pVec->nCount;/for (i=nIndex; i<nCount; i++)/pVec->pData[i] = pVec->pData[i+1];/return 1;/}//int _Vec##type##DelAll(struct _Vec##type * pVec, type e)/{/int skip;/int i;/type * p = pVec->pData;/int pos = -1;/int nCount = pVec->nCount;/for(i=0; i<nCount; i++)/if (*p++ == e)/{/pos = i;/break;/}/if (pos == -1) return 0;/skip = 1;/while (pos+skip < nCount)/{/if (pVec->pData[pos+skip] == e)/{/skip ++;/continue;/}/pVec->pData[pos] = pVec->pData[pos+skip];/pos ++;/}/pVec->nCount -= skip;/return 1;/}//int _Vec##type##Has(struct _Vec##type * pVec, type e)/{/type * p = pVec->pData;/int nCount = pVec->nCount;/int i;/for (i=0; i<nCount; i++)/if (*p++ == e)/return 1;/return 0;/}/void _Vec##type##Init(struct _Vec##type * pVec, int caption)/{/pVec->nCount = 0;/pVec->nCaption = caption;/pVec->pAlloc = _Vec##type##Alloc;/pVec->pFree = _Vec##type##Free;/pVec->pAdd = _Vec##type##Add;/pVec->pDel = _Vec##type##Del;/pVec->pDelAll = _Vec##type##DelAll;/pVec->pHas = _Vec##type##Has;/pVec->pEnsureCap = _Vec##type##EnsureCap;/pVec->pData = _Vec##type##Alloc(caption);/}#define VECTOR_DEFINE(type, vec) VECTOR_##type vec={_Vec##type##Init}#define VECTOR_TYPE(type) VECTOR_##type#define VECTOR_INIT_EX(pVec, caption) (pVec)->pInit((pVec), (caption))#define VECTOR_INIT(pVec) VECTOR_INIT_EX((pVec), 8)#define VECTOR_CLEAR(pVec) (pVec)->pFree((pVec)->pData); (pVec)->pData = 0; (pVec)->nCount = (pVec)->nCaption = 0#define VECTOR_ITEM(pVec, index) (pVec)->nCount > (index) ? (pVec)->pData + (index) : 0#define VECTOR_COUNT(pVec) ((pVec)->nCount)#define VECTOR_ADD(pVec, element) (pVec)->pAdd((pVec), (element))#define VECTOR_DEL(pVec, index) (pVec)->pDel((pVec), (index))#define VECTOR_DEL_ALL(pVec, element) (pVec)->pDelAll((pVec), (element))#define VECTOR_HAS(pVec, element) (pVec)->pHas((pVec), (element))#define VECTOR_ENSURE_CAP(pVec, caption) (pVec)->pEnsureCap((pVec), (caption))/************************************************************************//* sample#include <stdlib.h>#include <stdio.h>#include "vect.h"DECLARE_VECTOR(int);//定义int类型的VECTORIMPLEMENT_VECTOR(int);//实现int类型的VECTORvoid PrintVector(VECTOR_TYPE(int) * pVec);int main(int argc, char* argv[]){VECTOR_DEFINE(int, v);VECTOR_INIT(&v);//初始化VECTOR_ADD(&v, 1);//在末尾追加元素VECTOR_ADD(&v, 2);VECTOR_ADD(&v, 5);VECTOR_ADD(&v, 6);VECTOR_ADD(&v, 6);VECTOR_ADD(&v, 2);VECTOR_ADD(&v, 5);VECTOR_ADD(&v, 6);VECTOR_ADD(&v, 6);VECTOR_ADD(&v, 2);VECTOR_ADD(&v, 5);VECTOR_ADD(&v, 6);VECTOR_ADD(&v, 6);PrintVector(&v);VECTOR_DEL(&v, 1);//删除索引为1的元素VECTOR_DEL_ALL(&v, 6);//删除所有值为6的元素printf("%s值为5的元素。/r/n", VECTOR_HAS(&v, 5) ? "包含" : "不包含");printf("%s值为6的元素。/r/n", VECTOR_HAS(&v, 6) ? "包含" : "不包含");VECTOR_CLEAR(&v);//清理资源return 0;}void PrintVector(VECTOR_TYPE(int) * pVec){int i;for (i=0; i<VECTOR_COUNT(pVec); i++){int *p = VECTOR_ITEM(pVec, i);printf("%d ", *p);}printf("/r/n");}*//************************************************************************/#endif
- C/C++ 语言兼容的动态数组
- C语言动态数组
- c语言 动态数组
- C语言动态数组
- C语言动态数组
- C语言动态数组
- C语言动态数组
- C语言动态数组
- c语言动态数组
- C语言动态数组
- C 语言的动态多维数组
- C语言动态数组的完整实现
- C语言动态数组的完整实现
- C语言中动态数组的实现
- c语言中动态数组的实现
- C语言中动态数组的实现
- 二维数组的动态申请--c语言
- C语言中动态数组的实现
- Linux 内核内存检测工具 - Kmemcheck (转)
- Linux设备驱动开发环境的搭建
- 面向对象的三个基本特征(讲解)
- 雅虎前端优化14准则
- HtmlHelper解析与View (一)
- C/C++ 语言兼容的动态数组
- DirectShow技术简介
- platform_device和platform_driver
- 放假 也还是纠结!
- Camera ready format
- 一套价值8800元的EXCEL教程,包你一学就会。
- 关于Firefox3 不能调用DWR的问题
- Linux设备驱动的分层设计思想
- 一套价值8800元的EXCEL教程,包你一学就会。