实现了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. Qt入门(19)——自定义窗口部件

    我们介绍可以画自己的第一个自定义窗口部件.我们也加入了一个有用的键盘接口.我们添加了一个槽:setRange().        void setRange( int minVal, int maxV ...

  2. 数学 ZJOI 2012 数列

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ; st ...

  3. VS2010如何生成release文件

    点击生成-->配置管理器-->活动解决方案配置下拉菜单中选择release就行了,最后再编译一下就在相应的目录下生成了

  4. [Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II

    Closest Binary Search Tree Value  Given a non-empty binary search tree and a target value, find the ...

  5. maven上传自定义jar到本地仓库

    mvn install:install-file  -Dfile=D:/baidu/ueditor-1.1.1.jar  -DgroupId=com.baidu.ueditor  -Dartifact ...

  6. getting start with storm 翻译 第八章 part-1

    转载请注明出处:http://blog.csdn.net/lonelytrooper/article/details/12434915 第八章 事务性Topologies 在Storm中,正如本书前边 ...

  7. Unity3d个人信息开发流程

    1.首先先对需要交互的属性进行G/S,比如声明金币的属性 private int _coin; public String Coin{ get{ return _coin; } set{ return ...

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

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

  9. Python入门基础教程(儿童版) [分享一本入门级教程]

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1. 推荐书名 No Starch--Python for ...

  10. JAVA大数类

    JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...