通过C++实现了vector容器。直接上代码。必要的说明在注释中。
#include <iostream>
#define DEAFAULT_CAPACITY 4 //默认初始容量
using namespace std;
template <typename T> class Vector;
template <typename T>
ostream &operator<<(ostream &out, Vector<T> &vec); //运算符重载的声明
template <typename T> class Vector {
private:
int _size, _capacity;
T *_elem;
bool expand() { //扩容函数,在Vector满时扩容一倍,均摊复杂度O(1)
if (_size < _capacity) {
return false;
}
T *temp = _elem;
_elem = new T[_capacity * 2];
for (int i = 0; i < _size; i++) { //复制原Vector
_elem[i] = temp[i];
}
_capacity *= 2;
delete[] temp;
return true;
}
bool shrink() { //在Vector过大时进行缩减
if (_size * 4 > _capacity)
return false;
T *temp = _elem;
_elem = new T[_capacity / 2];
for (int i = 0; i < _size; i++) {
_elem[i] = temp[i];
}
_capacity /= 2;
delete[] temp;
return true;
}
public:
Vector(int cap = DEAFAULT_CAPACITY) { //两个构造函数
_elem = new T[cap];
_capacity = cap;
_size = 0;
}
Vector(int rank, T elem) {
_elem = new T[rank * 2];
for (int i = 0; i < rank; i++) {
_elem[i] = elem;
}
_size = rank;
_capacity = rank * 2;
}
~Vector() { delete[] _elem; } //析构函数
int size() const { return _size; } //加入const以示不做修改,下同
int capacity() const { return _capacity; }
T get(int rank) const { return _elem[rank]; }
int find(T elem, int left,
int right) const { //若找到,则返回该元素的秩,否则返回-1。
if (left >= right || left < 0 || right > _size) {
return -1;
}
for (int i = right - 1; i >= left; i--) {
if (elem == _elem[i]) {
return i;
}
}
return -1;
}
int find(T elem) const { return find(elem, 0, _size); }
//
int deduplicate() { //去重
int temp = _size;
int rank = 0;
while (rank < _size) {
int deduprank = find(_elem[rank], rank + 1, _size);
if (deduprank < 0)
rank++;
else {
remove(deduprank);
}
}
shrink();
return temp - _size;
}
int remove(int left, int right) { // Vector 段落删除
if (left >= right || left < 0 || right > _size) {
return 0;
}
while (right < _size) {
_elem[left] = _elem[right];
left++;
right++;
}
_size -= right - left;
shrink();
return right - left;
}
int remove(int rank) { // Vector 单元素删除,利用了段落删除函数
if (rank < 0 || rank >= _size) {
return 0;
}
remove(rank, rank + 1);
return 1;
}
int push_back(T elem) {
expand();
_elem[_size] = elem;
_size++;
return _size;
}
void push(T elem) {
expand();
for (int i = _size; i > 0; i--) {
_elem[i] = _elem[i - 1];
}
_elem[0] = elem;
_size++;
}
void sort(int left, int right) { //对Vector进行排序
if (left < 0 || right > _size || right - left < 2) {
return;
}
int i = left, j = right - 1;
T temp;
T flag = _elem[left];
while (i != j) {
while (_elem[j] >= flag && i < j)
j--;
while (_elem[i] <= flag && i < j)
i++;
if (j > i) {
temp = _elem[i];
_elem[i] = _elem[j];
_elem[j] = temp;
}
}
temp = flag;
_elem[left] = _elem[i];
_elem[i] = temp;
sort(left, i);
sort(j + 1, right);
}
void sort() { sort(0, _size); }
void put(int rank, T elem) { //对Vector元素进行修改
if (rank < 0 || rank >= _size)
return;
_elem[rank] = elem;
}
void insert(int rank, T elem) { //插入元素
if (rank < 0 || rank > _size) {
return;
}
expand();
for (int i = _size; i > rank; i--) {
_elem[i] = _elem[i - 1];
}
_elem[rank] = elem;
_size++;
}
//
//对运算符进行重载,下标运算符要重载为可修改的左值和右值两种形式
//其中,进行引用来重载为左值形式
T &operator[](int i) { return _elem[i]; }
T operator[](int i) const { return _elem[i]; }
//因为<>不能作为函数成员重载,故利用友元重载
friend ostream &operator<<<>(ostream &out, Vector<T> &vec);
};
//重载运算符<<
template <typename T> ostream &operator<<(ostream &out, Vector<T> &vec) {
for (int i = 0; i < vec.size(); i++) {
out << vec._elem[i] << " ";
}
return out;
}
//进行测试
int main() {
Vector<int> vec(9, 3);
cout << vec << endl;
vec.remove(2, 4);
cout << vec << endl;
vec.deduplicate();
cout << vec << endl;
vec.push(21);
vec.push(19);
cout << vec << endl;
vec.push_back(2);
cout << vec << endl;
vec.insert(0, 7);
cout << vec << endl;
vec.push(14);
cout << vec << endl;
vec.put(2, 18);
cout << vec << endl;
vec.sort();
cout << vec << endl;
cout << vec.size() << " " << vec.capacity() << endl;
cout << vec.find(21) << endl;
return 0;
}