实现了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. bzoj2730

    首先不难想到要先求割顶,求割顶的方法白书上有讲解由于是一个矿崩塌,所以假如一个连通块连接了两个以上割顶,那么这个连通块内显然是不用设出口的连接块只连接了一个割顶,那么出口可以设在这个连通块内任意位置由 ...

  2. HDU 5416 CRB and Tree

    题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...

  3. 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions

    题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...

  4. 【模拟】Codeforces 710B Optimal Point on a Line

    题目链接: http://codeforces.com/problemset/problem/710/B 题目大意: 给N个点的坐标,在X轴上找到最靠左的点使得这个点到N个点距离之和最小. 题目思路: ...

  5. kafka在zookeeper中的存储结构

    参考site:http://kafka.apache.org/documentation.html#impl_zookeeper 1.zookeeper客户端相关命令 在确保zookeeper服务启动 ...

  6. Tangled in Cables(Kruskal+map容器处理字符串)

    /** 题意:     给你两个城市之间的道路(无向图),求出需要的     电缆.如果大于所提供的,就输出Not enough ...     否则输出所需要的电缆长度.       输入:N (给 ...

  7. P - The Shortest Path in Nya Graph-hdu4725(双端队列+拆点)

    题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C.还有M条小路在两个点之间.问从第一个点走到第N个点最短路是多少... 可以考虑在每一层增加一个点,这个点到 ...

  8. D - Silver Cow Party

    题目大意: 在一个农场里面所有的牛都会来参加大牛举办的派对,不过农场的路都是单向的,而且每头牛都喜欢都最短的路程,那么问题来了,求出来来回花费时间最多的那头牛所用的时间... //////////// ...

  9. python通过SMTP发送邮件失败,报错505/535

    python通过SMTP发送邮件失败:错误1:smtplib.SMTPAuthenticationError: (550, b'User has no permission')    我们使用pyth ...

  10. TCP/UDP 、HTTP、IP 、socket 的关系。

    网络有上下分为7 层.物理层,数据链路层.网络层.会话层.应用层.传输层: IP协议位于网络层,IP和端口来控制网络流向: TCP.UDP是基于传输层.TCP保证三次握手.传递数据: UDP为不考虑是 ...