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 //也优先管 ...
随机推荐
- 【动画消消乐】HTML+CSS 自定义加载动画 064(currentColor的妙用!)
前言 Hello!小伙伴! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- 自我介绍ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 简介:因C语言结识编程,随后转入计算机专 ...
- 【LeetCode】145. 二叉树的后序遍历
145. 二叉树的后序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 后序 遍历. 示例 输入: [1,null,2,3] 1 \ 2 / 3 输 ...
- 基于小熊派Hi3861鸿蒙开发的IoT物联网学习【三】
软件定时器:是基于系统Tick时钟中断且由软件来模拟的定时器,当经过设定的Tick时钟计数值后会触发用户定义的回调函数.定时精度与系统Tick时钟的周期有关. 定时器运行机制: cmsis_os2的A ...
- 智能停车场车牌识别系统【python】
百度AI:https://ai.baidu.com 申请App_id 代码重点:pip install baidu_api from aip import AipOcr import os # 百 ...
- 第七篇 -- 常用界面组件的使用(QSlider和QProgressBar)
首先画个图 ui_proBar.py # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ui_ ...
- (Opencv02)图片展示
(Opencv02)图片展示 在程序里我们怎么把图片显示出来呢? 这里需要记一个自定义函数就好啦! def cv_show(name, img): cv2.imshow(name, img) ...
- 深入刨析tomcat 之---第15篇 对应20章, myAdmin案例代码
writedby 张艳涛 有没有和我一样做到第20章的?今天基本结束了本书的阅读.最后一个案例没有demo,那我写了一回,如果有需要的可以在本地试试 路径 D:\wksp_study\designbo ...
- 关于win7+cenos 7双系统安装
---恢复内容开始--- 1,cenos 0 7制作U盘启动 制作工具 http://pan.baidu.com/s/1nv9lpmp 镜像自备 2,安装centos 7 释放磁盘空间,如:20G.用 ...
- Hadoop 3.1.1 - Yarn 服务 - 总览
YARN 服务 总览 Yarn 服务框架为在 Yarn 原生环境里长时间运行的服务,提供了一流的支持和接口.简言之,它扮演了容器编排系统的角色,统一管理 Yarn 上运行的容器化服务.它同时支持 Do ...
- Server-Speaks-First 有点坑,Linkerd 2.10 中的协议检测和不透明端口
协议检测(Protocol detection),顾名思义,允许 Linkerd 自动检测 TCP 连接中使用的协议. Linkerd 的设计原则之一是"just work",协议 ...