vector自实现(一)
vector.h:
#ifndef __Vector__H__
#define __Vector__H__
typedef int Rank;
#define DEFAULT_CAPACITY 3 template<typename T> class Vector{
protected:
Rank _size; int _capacity; T*_elem;
void copyFrom(T const *A, Rank lo, Rank hi);
void expand();
void shrink();
void bubbleSort(Rank lo,Rank hi);
bool bubble(Rank lo,Rank hi);
void mergeSort(Rank lo,Rank hi);
void merge(Rank lo,Rank mi,Rank hi);
public:
Vector(int c = DEFAULT_CAPACITY,int s = 0, T v = 0)
{
_elem = new T[_capacity = s];
for (_size = 0; _size < s; _elem[_size++] = v);
}
Vector(T const *A, Rank n) { copyFrom(A, 0, n); }
Vector(T const *A, Rank lo, Rank hi) { copyFrom(A, lo, hi); }
Vector(Vector<T> const &V) { copyFrom(V._elem,0,V._size); }
Vector(Vector<T> const &V, Rank lo, Rank hi) { copyFrom(V._elem, lo, hi);}
~Vector(){delete[]_elem;} Rank size() const { return _size;}
bool empty() const { return !_size;}
Vector<T>& operator=(Vector<T> const&); T&operator[](Rank r) const; void unsort(Rank lo,Rank hi);
Rank find(T const& e,Rank lo,Rank hi) const;
Rank insert(Rank r,T const& e);
int remove(Rank lo,Rank hi);
T remove(Rank r);
int deduplicate();
int disordered() const; int uniquify(); void traverse(void(*)(T&));
template<typename VST> void traverse(VST&); static Rank binSearch(T* A,T const& e,Rank lo,Rank hi);
static Rank binSearch2(T* A, T const& e, Rank lo, Rank hi);
static Rank binSearch3(T* A, T const& e, Rank lo, Rank hi);
};
#endif
vector.cpp:
#include"Vector.h"
template<typename T>
void Vector<T>::copyFrom(T const * A, Rank lo, Rank hi)
{
_elem = new T[_capacity=2*(hi-lo)];
_size = 0;
while (lo < hi)
{
_elem[_size++] = A[lo++];
}
} template<typename T>
void Vector<T>::expand()
{
if (_size < _capacity)return;
if (_capacity < DEFAULT_CAPACITY) { _capacity = DEFAULT_CAPACITY; }
T*oldElem = _elem;
_elem = new T[_capacity<<=1];
for (int i = 0; i < _size; i++)
{
_elem[i] = oldElem[i];
}
delete[]oldElem;
} template<typename T>
void Vector<T>::shrink()
{
if (_capacity < DEFAULT_CAPACITY << 1)return;
if (_size << 2 > _capacity)return;
T *oldElem = _elem;
_elem = new T[_capacity>>=1];
for(int i = 0; i < _size; i++)
{
_elem[i] = oldElem[i];
}
delete[]oldElem;
} template<typename T>
void Vector<T>::bubbleSort(Rank lo, Rank hi)
{
while (!bubble(lo,hi--));
} template<typename T>
bool Vector<T>::bubble(Rank lo, Rank hi)
{
bool sorted = true;
while (++lo < hi)
{
if(_elem[lo - 1] < _elem[lo])
{
sorted = false;
swap(_elem[lo-1],_elem[lo]);
}
}
return sorted;
} template<typename T>
void Vector<T>::mergeSort(Rank lo, Rank hi)
{
if (hi - lo < 2) return;
int mi = (lo + hi) / 2;
mergeSort(lo,mi);
mergeSort(mi,hi);
merge(lo,mi,hi);
} template<typename T>
void Vector<T>::merge(Rank lo, Rank mi,Rank hi)
{
T *A = _elem + lo;
int lb = mi - lo;
T* B = new T[lb];
for (int i = 0; i < lb; i++) B[i] = A[i];
int rb = hi - mi;
T *C = _elem + mi;
for (Rank i=0,int j = 0, int k = 0; (j < lb) || (k < rb);)
{
if ((j<lb)&&(k>rb||(B[j] < C[k])))
{
A[i++] = B[j++];
}
if ((k<rb)&&(j>lb||(C[k]<=B[j])))
{
A[i++] = C[k++];
}
}
delete[] B; } template<typename T>
Vector<T>& Vector<T>::operator=(Vector<T> const & V)
{
if (_elem) delete[]_elem;
copyFrom(V._elem,0,V._size);
return *this;
} template<typename T>
T & Vector<T>::operator[](Rank r) const
{
return _elem[r];
} template<typename T>
void Vector<T>::unsort(Rank lo, Rank hi)
{
T* V = _elem + lo;
for(Rank i = hi - lo; i >= 0; i--)
{
swap(V[i-1] = V[rand()%i]);
}
} template<typename T>
Rank Vector<T>::find(T const & e, Rank lo, Rank hi) const
{
while((lo < hi--)&&(e!=_elem[hi]));
return hi;
} template<typename T>
Rank Vector<T>::insert(Rank r, T const & e)
{
expand();
for (int i = _size; i > r; i--)
{
_elem[i] = _elem[i-1];
}
_elem[r] = e;
_size++;
return r;
} template<typename T>
int Vector<T>::remove(Rank lo, Rank hi)
{
if (lo == hi)return 0;
while (hi < _size)
{
_elem[lo++] = _elem[hi++];
}
_size = lo;
shrink();
return hi - lo;
} template<typename T>
T Vector<T>::remove(Rank r)
{
T e = _elem[r];
remove(r,r+1);
return e;
} template<typename T>
int Vector<T>::deduplicate()
{
int oldsize = _size;
Rank i = 1;
while (i < _size)
{
(find(_elem[i], 0, _size) < 0) ? i++ : remove(i);
} return oldsize-_size; } template<typename T>
int Vector<T>::disordered() const
{
int n = 0;
for (int i = 1; i < _size; i++)
{
if(_elem[i - 1] > _elem[i])
{
n++;
}
}
return n;
} template<typename T>
int Vector<T>::uniquify()
{
Rank i = 0, j = 0;
while (++j < _size)
{
if (_elem[i] != _elem[j])
{
_elem[++i] = _elem[j];
}
}
_size = ++i;
shrink();
return j - i;
} template<typename T>
Rank Vector<T>::binSearch(T * A, T const & e, Rank lo, Rank hi)
{
while (lo < hi)
{
Rank mi = (lo + hi) >> 1;
if (e > A[mi])
{
lo = mi+1;
}
else if (e < A[mi])
{
hi = mi;
}
else {
return mi;
}
}
return -1;
} template<typename T>
Rank Vector<T>::binSearch2(T * A, T const & e, Rank lo, Rank hi)
{
while (1 < (hi - lo))
{
Rank mi = (lo + hi) >> 1;
(e < A[mi]) ? hi = mi : lo = mi;
}
return (e == A[lo]) ? lo : -1;
} template<typename T>
Rank Vector<T>::binSearch3(T * A, T const & e, Rank lo, Rank hi)
{
while (lo < hi)
{
Rank mi = (lo + hi) >> 1;
(e < A[mi]) ? hi = mi : lo = mi + 1;
}
return --lo;
} template<typename T>
void Vector<T>::traverse(void(*visit)(T &))
{
for (int i = 0; i < _size; i++)
{
visit(_elem[i]);
}
} template<typename T>
template<typename VST>
void Vector<T>::traverse(VST &visit)
{
for (int i = 0; i < _size; i++)
{
visit(_elem[i]);
}
}
vector自实现(一)的更多相关文章
- c++ vector 使用
1. 包含一个头文件: 1 #include <vector> 2. 申明及初始化: std::vector<int> first; // empty vector of in ...
- Vector Tile
Mapbox Vector Tile Specification A specification for encoding tiled vector data. <?XML:NAMESPACE ...
- ArrayList、Vector、LinkedList的区别联系?
1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...
- ArrayList、Vector、HashMap、HashSet的默认初始容量、加载因子、扩容增量
当底层实现涉及到扩容时,容器或重新分配一段更大的连续内存(如果是离散分配则不需要重新分配,离散分配都是插入新元素时动态分配内存),要将容器原来的数据全部复制到新的内存上,这无疑使效率大大降低. 加载因 ...
- Java中Vector和ArrayList的区别
首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...
- C++使用vector
#include <iostream> #include <string> #include <vector> using namespace std; void ...
- [LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- C++ 数组array与vector的比较
转:http://blog.csdn.net/yukin_xue/article/details/7391897 1. array 定义的时候必须定义数组的元素个数;而vector 不需要: 且只能包 ...
- vector定义初始化
头文件 #include<vector> using std::vector; vector<T> v1; vector<T> v2(v1); vector< ...
- vector迭代器用法
#include<iostream> #include<vector> using namespace std; int main() { vector<int> ...
随机推荐
- 资源分享 | PyTea:不用运行代码,静态分析pytorch模型的错误
前言 本文介绍一个Pytorch模型的静态分析器 PyTea,它不需要运行代码,即可在几秒钟之内扫描分析出模型中的张量形状错误.文末附使用方法. 本文转载自机器之心 编辑:CV技 ...
- 【LeetCode】1402. 做菜顺序 Reducing Dishes
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcode ...
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- Variational Inference with Normalizing Flow
目录 概 主要内容 一些合适的可逆变换 代码 Rezende D., Mohamed S. Variational Inference with Normalizing Flow. ICML, 201 ...
- matplotlib 进阶之Constrained Layout Guide
目录 简单的例子 Colorbars Suptitle Legends Padding and Spacing spacing with colobars rcParams Use with Grid ...
- 替代瑞昱RTD2166|pin对pin替代RTD2166|CS5202芯片
替代瑞昱RTD2166,pin对pin替代RTD2166,外围器件少,设计版框尺寸小,整套方案成本BOM更低. 一. CS5202功能概述 CS5202是一款DP端口到VGA转换器,它结合了DP输入 ...
- Android开发布局 案例一
权重:就是在布局界面中所占的比例 实践案例: <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- 物理CPU,物理CPU内核,逻辑CPU概念详解
1.说明 CPU(Central Processing Unit)是中央处理单元, 本文介绍物理CPU,物理CPU内核,逻辑CPU, 以及他们三者之间的关系, 一个物理CPU可以有1个或者多个物理内核 ...
- .net core集成使用EasyNetQ来使用rabbitmq
之前有写到一篇介绍EasyNetQ的博文(C# .net 使用rabbitmq消息队列--EasyNetQ插件介绍),所以本文从.net core的角度去继承使用EasyNetQ,而用法类似于之前集成 ...
- [GDOI2021 Day2T1] 宝石
题目大意 \(n\)个点的树, 树上每一个点有一个宝石\(w_i\), 给出一个固定的数字不重复的序列\(p_i\)和一些询问\(u_i, v_i\), 对于每一个询问求出\(u_i\)到\(v_i\ ...