C语言实现类似C++的容器vector
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <string.h>
5 typedef int DataType;
6 typedef struct array
7 {
8 DataType *Data;
9 int size,max_size;
10 void (*Constructor)(struct array *);//构造函数
11 void (*Input)(DataType ,struct array *);//输入数据
12 int (*get_array_size)(struct array *);//获取arr的大小
13 int (*return_index_value)(struct array *,int);//返回下标为index的值
14 void (*print)(struct array *);//打印结果
15 void (*Destructor)(struct array *);//析构函数
16 }Array;
17
18 void Init(Array *this);
19 void _print(struct array *this);
20 void _constructor(Array *this);
21 void _denstructor(Array *this);
22 void _input(DataType data,Array *this);
23 int _get_szie(Array *this);
24 int _return_index_value(Arrary *this,int index);
25
26 void Init(Array *this)
27 {
28 this->Input =_input;
29 this->print =_print;
30 this->get_array_size = _get_size;
31 this->return_index_value = _return_index_value;
32 this->Constructor =_constructor;
33 this->Destructor =_denstructor;
34 this->Constructor(this);
35 }
36
37 void _constructor(Array *this)
38 {
39 this->size=0;
40 this->max_size=10;
41 this->Data=(DataType *)malloc(this->max_size*sizeof(DataType));
42 memset(this->Data,0,10);
43 }
44
45 void _input(DataType data, Array *this)
46 {
47 int i;
48 DataType *ptr;
49
50 if(this->size >= this->max_size)
51 {
52 this->max_size +=10;
53 ptr=(DataType *)malloc(this->max_size*sizeof(DataType));
54 for(i=0;i<this->size;i++)
55 ptr[i]=this->Data[i];
56 free(this->Data);
57 this->Data=ptr;
58 }
59 this->Data[this->size]=data;
60 this->size +=1 ;
61 }
62
63 void _print(struct array *this)
64 {
65 assert(this != NULL);
66 struct array *ptr=this;
67 int i=0;
68 for(i=0;i<ptr->size;i++)
69 printf("data is %d\n",ptr->Data[i]);
70
71 return ;
72 }
73 int _get_array_size(Array *this)
74 {
75 assert(this != NULL);
76 return this->size+1;
77 }
78 int _return_index_value(Array *this,int index)
79 {
80 assert(this != NULL);
81 return (this->Data[index]);
82 }
83 void _denstructor(Array *this)
84 {
85 int i=0;
86 assert(this != NULL);
87 for(i=0;i<this->max_size;i++)
88 this->Data[i]=0;
89 free(this->Data);
90 }
91
92 int main()
93 {
94 Array MyArray;
95
96 Init(&MyArray); //使用对象前必须初始化,间接调用构造函数
97 // MyArray.Data[]={1,2,3,4,5};
98 MyArray.Input(1,&MyArray);
99 MyArray.Input(2,&MyArray);
100 MyArray.Input(3,&MyArray);
101 MyArray.Input(4,&MyArray);
102 MyArray.Input('5',&MyArray);
103 MyArray.print(&MyArray);
104 printf("the array size is :%d\n",MyArray.get_array_size(&MyAarray));
105 printf("the index value in array is:%d\n",MyArray.return_index_value(&MyArray,3));
106 MyArray.Destructor(&MyArray); //使用对象后必须显式调用析构函数
107
108 return 0;
109 }
C语言实现类似C++的容器vector的更多相关文章
- 2.2 C语言_实现数据容器vector(排序功能)
上一节我们说到我们己经实现了一般Vector可以做到的自动扩充,告诉随机存取,那么现在我们需要完成vector的一个排序的功能. 排序算法我们网上一百度哇~~!很常见的就有8大排序算法: 1.选择排序 ...
- C语言如何实现继承及容器
继承的概念 继承是面向对象软件技术当中的一个概念,与多态.封装共为面向对象的三个基本特征.继承可以使得子类具有父类的属性和方法或者重新定义,追加属性和方法. 面向对象中的重要概念就是类,在我们熟知的编 ...
- C++线性序列容器<vector>简单总结
C++线性序列容器<vector>简单总结 vector是一个长度可变的数组,使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加:Vector类提供额外的方法来增加.删除 ...
- [C++]STL容器Vector的内存释放
直接抛出两句话,说明到底应该如何释放Vector占用的内存. “vector的clear不影响capacity,你应该swap一个空的vector.” <Effective STL>中的“ ...
- STL标准库-容器-vector
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. 向量容器vector是一个动态数组,内存连续,它是动态分配内存,且每次扩张的原来的二倍. 他的结构如下 一 定义 vector ...
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
序列性容器::(vector和list和deque) erase迭代器不仅使所有指向被删元素的迭代器失效,而且使被 删元素之后的所有迭代器失效,所以不能使用erase(iter++)的方 式, ...
- 从零开始写STL—容器—vector
从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...
- C++进阶 STL(1) 第一天 [容器,算法,迭代器] string容器 vector容器 deque容器
课程大纲 02实现基本原理 容器,算法,迭代器 教室:容器 人:元素 教室对于楼:容器 序列式容器: 容器元素在容器中的位置是由进入容器的时间和地点来决定 序列式容器 关联式容器: 教室中 按年龄排座 ...
- C++ 顺序容器(vector,list、deque,stack,queue)
顺序容器的种类有:vector,list.deque 顺序容器适配器: stack //先进后出 栈 queue //先进先出 队列 priority_queue //也优先管 ...
随机推荐
- 简单梳理 ES6 函数
箭头函数 箭头函数提供了一种更加简洁的函数书写方式.基本语法是 参数 => 函数体 基本用法: var f = v => v; //等价于 var f = function(a){ ret ...
- [刘阳Java]_BeanNameViewResolver视图解析器_第8讲
BeanNameViewResolver:这个视图解析器跟XmlViewResolver有点类似,也是通过把返回的逻辑视图名称去匹配定义好的视图bean对象.它要求视图bean对象都定义在Spring ...
- 微信小程序云开发-数据库-用户更新数据并提交
一.wxml增加input输入框和[更新商品价格]按钮 在商品详情页新增[更新商品价格]按钮,wxml新增部分代码,input绑定事件,用于获取用户输入的内容.按钮绑定事件,用于更新商品价格. 二. ...
- debian 9 pycharm安装
官网下载PyCharm的tar.gz格式 使用命令进行解压:tar -xvzf pycharm.tar.gz 解压后将pycharm文件夹移动到/usr/local/lib/目录下 进入pycharm ...
- SAML 2.0 流程分析(2)
- ELK:match 的底层转换
在ES中,执行match搜索的时候,ES底层通常都会对搜索条件进行底层转换,来实现最终的搜索结果.如: GET /student/java/_search { "query": { ...
- 构建前端第4篇之---使用css用法 height
张艳涛 写于2021-1-20 height: 100%; What: html的元素标签,例如 <html>,<body>,<div>都有height的css属 ...
- 我眼中的java线程池实现原理
最近在看java线程池实现方面的源码,在此做个小结,因为网上关于线程池源码分析的博客挺多的,我也不打算重复造轮子啦,仅仅用纯语言描述的方式做做总结啦! 个人认为要想理解清楚java线程池实现原理,明白 ...
- Apache ActiveMQ(cve-2015-5254)
影响版本 Apache ActiveMQ 5.13.0之前5.x版本中存在安全漏洞 复现 使用工具执行命令 工具地址 https://github.com/matthiaskaiser/jmet/re ...
- GoldenEye-v1靶机
仅供个人娱乐 靶机信息 下载地址:https://pan.baidu.com/s/1dzs_qx-YwYHk-vanbUeIxQ 一.主机扫描 二.信息收集 三.漏洞的查找和利用 boris I ...