用c语言实现c++ array容器类
来源:互联网 发布:51talk真实评价知乎 编辑:程序博客网 时间:2024/06/06 01:08
通过函数指针,可以巧妙实现面向对象的东西.
//vector.h#ifndef __VACTOR_H__#define __VECTOR_H__/*======================STRUCT=========================*/#define DEFAULT_ARRAY__SIZE 10typedef int Datatype;typedef struct Array{ Datatype *data; /**< pointer to the array object */ int size; /**< current array's size */ int maxsize; /**< default array's maxsize*/ void(*constructor)(struct Array *pthis); /**< pointer to constructor*/ void(*add_value)(struct Array *pthis, Datatype data); /**< funciton pointer: add the data into the array*/ void(*print)(struct Array *pthis); /**< function pointer:print all of the data of array*/ int(*get_array_size)(struct Array *pthis); /**< function pointer: return current array size*/ int(*return_index_of_firstmatch)(struct Array *pthis, Datatype data); /**< function pointer: return index of the first match data*/ int (*return_index_value)(struct Array *pthis, Datatype data); /**< function pointer: return the value of the index*/ void(*descontructor)(struct Array *pthis); /**< function pointer: descontructor*/}Array;/*========================FUNCTION==============================*//** * @brief initialize struct Array * @param pointer to struct Array */void array_init(Array *pthis);/** * @brief __constructor * @param pointer to struct Array */void __constructor(Array *pthis);/** * @brief add value into the array * @param pointer to struct Array * @param the data to be added */void __add_value(Array *pthis, Datatype data);/** * @brief print the array * @param pointer to struct Array */void __print(Array *pthis);/** * @brief get array size * @param pointer to struct Array * @return size */int __get_array_size(Array *pthis);/** * @brief return the inde of first match data of the array * @param pointer to struct Array * @param the data to find * @return >=0 on success, represent the index * -1 on failure */int __return_index_of_firstmatch(Array *pthis, Datatype data);/** * @brief return the value of index * @param pointer to struct Array * @param index * @return value */int __return_index_value(Array *pthis, int index);/** * @brief destructor * @param pointer to struct Array */void __destructor(Array *pthis);#endif
//vector.c#include"vector.h"#include<stdio.h>#include<assert.h>#include<string.h>#include<malloc.h>void array_init(Array *pthis){ assert(pthis); /*initialize the function pointer*/ pthis->constructor = __constructor; pthis->descontructor = __destructor; pthis->add_value = __add_value; pthis->print = __print; pthis->get_array_size = __get_array_size; pthis->return_index_value = __return_index_value; pthis->return_index_of_firstmatch = __return_index_of_firstmatch; /* implicit call the constructor */ pthis->constructor(pthis);}void __constructor(Array *pthis){ assert(pthis); pthis->size = 0; pthis->maxsize = DEFAULT_ARRAY__SIZE; pthis->data = (Datatype *)malloc(sizeof(Datatype) * pthis->maxsize); memset(pthis->data, 0, sizeof(Datatype) * pthis->maxsize);}void __add_value(Array *pthis, Datatype data){ assert(pthis); if(pthis->size == pthis->maxsize) { pthis->maxsize += DEFAULT_ARRAY__SIZE; Datatype * temp = (Datatype *)malloc(sizeof(Datatype) * pthis->maxsize); int i; for(i = 0; i < pthis->size; i++) temp[i] = pthis->data[i]; free(pthis->data); pthis->data = temp; } pthis->data[pthis->size] = data; pthis->size++;}void __print(Array *pthis){ int i; for(i = 0; i < pthis->size; i++) printf("%d ", pthis->data[i]); printf("\n");}int __get_array_size(Array *pthis){ return pthis->size;}int __return_index_of_firstmatch(Array *pthis, Datatype data){ int i; for(i = 0; i < pthis->size; i++) { if(pthis->data[i] == data) return i + 1; } return -1;}int __return_index_value(Array *pthis, int index){ return pthis->data[index];}/* destructor the object*/void __destructor(Array *pthis){ free(pthis->data);}
//main.c#include<stdio.h>#include"vector.h"int main(){ Array arr; array_init(&arr); int i; for(i = 0; i < 25; i++) { arr.add_value(&arr, i); } arr.print(&arr); printf("current array size:%d\n", arr.get_array_size(&arr)); /* if arr.return_index_of_firstmatch(&arr,5) return -1 ,that means could not exist*/ printf("%d is in the %dth of the array\n", 5, arr.return_index_of_firstmatch(&arr,5)); printf("%d index of the array is value %d\n", 5, arr.return_index_value(&arr, 5)); arr.descontructor(&arr); return 0;}
0 0
- 用c语言实现c++ array容器类
- C语言--容器的实现。
- C语言实现一个泛型容器
- C语言-Array
- C语言-Array(High)
- C语言实现类
- [leetcode](Remove Duplicates from Sorted Array II C语言实现)
- 【C++】实现容器Vector
- LeetCode::Rotate Array C语言
- [leetcode]Rotate Array C语言
- C语言二维数组Array[][]
- C语言实现类似于C++的Vector容器
- 用C语言实现有限状态机
- 用C语言实现有限状态机
- 用C语言实现哈夫曼树
- 用C语言实现myshell
- 用C语言实现九九乘法表
- 用c语言实现栈
- 开源移动端IM比较SipDroid,IMSDroid,CSipsimple,Linphone,webrtc
- 最小二乘法和极大似然估计
- WPF-DataGrid中CheckBox实现全选与非全选
- Net-snmp安装配置
- Linux 系统内核的调试
- 用c语言实现c++ array容器类
- 最大熵模型中的数学推导
- 如何搭建一个独立博客——简明Github Pages与Hexo教程
- jquery UI学习笔记-知问前端之日历控件
- iOS开发隐藏键盘方法总结
- iOS 8创建交互式通知
- 国达色谱金银花(木犀草苷)专用色谱柱
- 进程间通信之共享内存
- Android.mk常用笔记