模拟vector
#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的更多相关文章
- Tournament Chart【模拟+vector+map+string】
Tournament Chart 传送门:链接 来源:UPC10889 题目描述 In 21XX, an annual programming contest, Japan Algorithmist ...
- 2015年第六届蓝桥杯省赛T10 生命之树(树形dp+Java模拟vector)
生命之树 在X森林里,上帝创建了生命之树. 他给每棵树的每个节点(叶子也称为一个节点)上,都标了一个整数,代表这个点的和谐值. 上帝要在这棵树内选出一个非空节点集S,使得对于S中的任意两个点a,b,都 ...
- pat甲级 1154 Vertex Coloring (25 分)
A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...
- (原创)动态内存管理练习 C++ std::vector<int> 模拟实现
今天看了primer C++的 “动态内存管理类”章节,里面的例子是模拟实现std::vector<std::string>的功能. 照抄之后发现编译不通过,有个库函数调用错误,就参考着自 ...
- hdu 5071 vector操作恶心模拟
http://acm.hdu.edu.cn/showproblem.php?pid=5071 对于每一个窗口,有两个属性:优先级+说过的单词数,支持8个操作:新建窗口,关闭窗口并输出信息,聊天(置顶窗 ...
- UVa 12100 Printer Queue(queue或者vector模拟队列)
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- <泛> STL - vector 模拟实现
今天为大家带来一个模拟STL-vector的模板实现代码. 首先看一下测试结果,之后再为大家呈现设计 测试效果 测试代码 #include<iostream> #include<ve ...
- SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )
图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...
随机推荐
- 平衡树(Splay):Splaytree POJ 3580 SuperMemo
SuperMemo Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...
- C#怎么得到主机名,IP,MAC
一:基础知识 a: Dns 类 提供简单的域名解析功能. Dns 类是一个静态类,它从 Internet 域名系统 (DNS) 检索关于特定主机的信息. 在 IPHostEntry 类的实例中返回来自 ...
- Summary Ranges —— LeetCode
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java Topology Suite (JTS)与空间数据模型
JTS是Java的处理地理数据的API,它提供以下功能: 实现了OGC关于简单要素SQL查询规范定义的空间数据模型 一个完整的.一致的.基本的二维空间算法的实现,包括二元运算(例如touch和over ...
- UVA 11478 Halum(差分约束)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 [思路] 差分约束系统. 设结点u上的操作和为sum[u] ...
- Postman interceptor
安装 下载地址: Postman Interceptor Chrome插件下载 1. 下载的是一个crx文件. 2. 在谷歌中打开: chrome://extensions/ 3. 拖动cfx文件到 ...
- Git 的优点
1. 快速 如果你每移动一下鼠标都要等待五秒,是不是很受不了?版本控制也是一样的,每一个命令多那么几秒钟,一天下来也会浪费你不少时间.Git的操作非常快速,你可以把时间用在别的更有意义的地方. 2. ...
- multipath.conf
# This is a basic configuration file with some examples, for device mapper# multipath.# For a comple ...
- angularJS promise $q
Promise 一 介绍 1.什么是promise 我们知道JavaScript语言的执行环境是“单线程”,所谓单线程,就是一次只能够执行一个任务,如果有多个任务的话就要排队,前面一个任务完成后才可以 ...
- swiper.js 移动端触摸滑动插件
API链接: http://www.swiper.com.cn/api/start/2014/1218/140.html <body> <div class="swiper ...