实现了vector的模板,insert, erase, push_back, iterator
#include<iostream>
#include<string.h>
#include<stdio.h>
#include <stdlib.h>
using namespace std;
template <typename T>
class Vector{
public:
int length = ;
int size = ;
T *a = (T*) malloc(sizeof(T));;
T &operator[](int i){
return a[i];
}
T push_back(T in){
if(length == size){
T *b;
b = (T*) malloc(size**sizeof(T));
for(int i = ; i < size; i++){
b[i] = a[i];
}
delete(a);
a = b;
size *= ;
}
a[length++] = in;
}
T* begin(){
return a;
}
T* end(){
return a+length;
}
typedef T* iterator;
void clear(){
length = ;
}
int insert(int num, T b){
if(num >= length) return -;
for(int i = length; i >= num; i--){
a[i] = a[i-];
}
a[num] = b;
length++;
return ;
}
int erase(int num){
if(num > length){
return -;
}
for(int i = num; i <length-; i++){
a[i] = a[i]+;
}
length --;
return ;
}
};
class cl{
public: int a, b;
cl(){a = , b = ;};
};
Vector<int> v;
Vector<cl> c;
int main(){
cl a;
c.push_back(a);
Vector<cl>::iterator It;
for(It=c.begin();It!=c.end();It++)
cout<<It->a<<" "<<It->b;
cout<<endl; int *b = (int*) malloc();
printf("%d", b[]);
for(int i = ; i < ; i++){
v.push_back(i);
}
for(int i = ; i < ; i++){
printf("%d ", v[i]);
}puts("");
Vector<int>::iterator it;
for(it=v.begin();it!=v.end();it++)
cout<<*it<<" ";
cout<<endl;
printf("%d %d\n", v.size, v.length);
v.insert(, );
for(it=v.begin();it!=v.end();it++)
cout<<*it<<" ";
cout<<endl;
printf("%d %d\n", v.size, v.length);
v.erase();
v.erase();
for(it=v.begin();it!=v.end();it++)
cout<<*it<<" ";
cout<<endl;
printf("%d %d\n", v.size, v.length); }

模拟vector的更多相关文章

  1. Tournament Chart【模拟+vector+map+string】

    Tournament Chart 传送门:链接  来源:UPC10889 题目描述 In 21XX, an annual programming contest, Japan Algorithmist ...

  2. 2015年第六届蓝桥杯省赛T10 生命之树(树形dp+Java模拟vector)

    生命之树 在X森林里,上帝创建了生命之树. 他给每棵树的每个节点(叶子也称为一个节点)上,都标了一个整数,代表这个点的和谐值. 上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点a,b,都 ...

  3. pat甲级 1154 Vertex Coloring (25 分)

    A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...

  4. uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟

    挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...

  5. (原创)动态内存管理练习 C++ std::vector<int> 模拟实现

    今天看了primer C++的 “动态内存管理类”章节,里面的例子是模拟实现std::vector<std::string>的功能. 照抄之后发现编译不通过,有个库函数调用错误,就参考着自 ...

  6. hdu 5071 vector操作恶心模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=5071 对于每一个窗口,有两个属性:优先级+说过的单词数,支持8个操作:新建窗口,关闭窗口并输出信息,聊天(置顶窗 ...

  7. UVa 12100 Printer Queue(queue或者vector模拟队列)

    The only printer in the computer science students' union is experiencing an extremely heavy workload ...

  8. <泛> STL - vector 模拟实现

    今天为大家带来一个模拟STL-vector的模板实现代码. 首先看一下测试结果,之后再为大家呈现设计 测试效果 测试代码 #include<iostream> #include<ve ...

  9. SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )

    图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...

随机推荐

  1. [转]是String,StringBuffer还是StringBuilder?

    原文链接. 相信大家对 String 和 StringBuffer 的区别也已经很了解了,但是估计还是会有很多同志对这两个类的工作原理有些不清楚的地方,今天我在这里重新把这个概念给大家复习一下,顺便牵 ...

  2. 数据结构(树链剖分):BZOJ 4034: [HAOI2015]T2

    Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中 ...

  3. 老罗android开发视频教程学习完了

    让我学到了android的文件结构,事件窗口数据传递,百度地图开发很感谢

  4. 使用Multipath进行多链路聚合并对聚合后的设备固定命名

    使用Multipath进行多链路聚合并对聚合后的设备固定命名 1.启用Multipath: (1)启动multipathd服务 #service multipathd start 或者 #/etc/i ...

  5. 分布式锁 基于Redis

    分布式锁的实现(基于Redis) 参考:http://www.jb51.net/article/75439.htm http://www.linuxidc.com/Linux/2015-01/1118 ...

  6. python获取本机IP、mac地址、计算机名

    在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...

  7. [PWA] 18. Clean the photo cache

    We cannot let photo always keep caching new data without clean the old data. If message is not displ ...

  8. Unity3d Awake、OnEnable、Start生命周期

    Unity3d,Awake.OnEnable.Start,都是游戏开始运行前,所运行的方法. GameObject的Activity为true,脚本的enable为true时,其先后顺序为:Awake ...

  9. Swift: The Basics

    Swift是类型安全的语言: Swift introduces optional types, which handle the absence of a value. Optional say ei ...

  10. [转] Nginx + CGI/FastCGI + C/Cpp

    接着上篇<Nginx安装与使用>,本篇介绍CGI/FASTCGI的原理.及如何使用C/C++编写简单的CGI/FastCGI,最后将CGI/FASTCGI部署到nginx.内容大纲如下: ...