代码实现
此处实现直接放在了头文件中
环境:VS2017
#pragma once
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
template<typename T>
class hn_vector
{
public:
//构造函数
hn_vector(int size = 0) :theSize(size), theCapacity(size + INIT_CAPACITY)
{
cout << "进入构造函数" << endl;
data = new T[theCapacity];
}
//复制构造函数
hn_vector(const hn_vector& other) :theSize(0), theCapacity(0), data(nullptr)
{
cout << "进入复制构造" << endl;
//theSize = other.theSize;
//theCapacity = other.theCapacity;
//data = new T[theCapacity];
//cout << "复制内存" << endl;
//memcpy(data, other.data, theSize * sizeof(T));//使用内存复制函数
*this = other;//也可以使用重载赋值运算符来实现
}
//重载赋值函数
hn_vector& operator=(const hn_vector& other)
{
cout << "进入重载赋值运算符函数" << endl;
if (this == &other)
return *this;
else
{
delete[] data;
theSize = other.theSize;
theCapacity = other.theCapacity;
data = new T[theCapacity];
for (int i = 0; i < theSize; i++)
{
data[i] = other.data[i];
}
return *this;
}
}
//重载下标运算符
T& operator[](int index)
{
return data[index];//非局部对象,可以返回引用,提高效率
}
//重新分配空间
void reServe(int newCapacity)
{
cout << "重新分配空间" << endl;
if (newCapacity<=theCapacity)
{
return;
}
theCapacity = newCapacity;
T* temp = data;
data = new T[newCapacity];
memcpy(data, temp, theSize * sizeof(T));
//for (int i = 0; i < theSize; i++)
//{
// data[i] = temp[i];
//}
delete [] temp;
}
//push_back函数
void push_back(T value)
{
cout << "进入push_back函数" << endl;
if (theCapacity==theSize)
{
reServe(2 * theCapacity + 1);
}
data[theSize++] = value;
}
int size()
{
return theSize;
}
int capacity()
{
return theCapacity;
}
//仅仅为了对比析构地址,实际不应有此成员函数
T* get_address()
{
return data;
}
void assign(int num,T value)
{
if (num >= theCapacity)
{
reServe(theCapacity * 2 + 1);
}
theSize = 0;//若是之前assign了少于num的值被覆盖,多于num的值还在,但是theSize使我们访问不到,就好像不在了一样。
for (int i=0; i < num; i++)
{
data[i] = value;
theSize++;
}
}
T at(int index)
{
if (index >= theSize)
{
cerr << "超出边界" << endl;
return -1;
}
return data[index];
}
T& back()
{
return data[theSize - 1];
}
T& begin()
{
return data[0];
}
~hn_vector() {
cout<<"进入析构,释放空间"<<endl;
if (data != NULL)
{
cout<<"this.data address:"<<data<<endl;
delete[] data;
}
}
private:
const int INIT_CAPACITY = 5;
int theSize;//已用大小
int theCapacity;//总大小
T* data;
public:
T* iterator;
};
测试文件
#include "pch.h"
#include <iostream>
#include "hn_vector.h"
using namespace std;
int main()
{
hn_vector<int> v1;
cout <<"size:"<< v1.size() << endl;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.push_back(6);
cout<<"v1 size:"<<v1.size()<<" v1 capacity:"<<v1.capacity()<<" v1 address:"<<&v1<<" v1.data address:"<<v1.get_address()<<endl;
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
cout << "===============" << endl;
hn_vector<int> v2(v1);
cout << "v2 size:" << v2.size() << " v2 capacity:" << v2.capacity() << " v2 address:" << &v2 << " v2.data address:" << v2.get_address() << endl;
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << " ";
}
cout << endl;
cout << "===============" << endl;
hn_vector<int> v3;
v3= v1;
cout << "v3 size:" << v3.size() << " v3 capacity:" << v3.capacity() << " v3 address:" << &v3 << " v3.data address:" << v3.get_address() << endl;
for (int i = 0; i < v3.size(); i++)
{
cout << v3[i] << " ";
}
//cout << "============TEST====FUNC===============" << endl;
//hn_vector<int> v4;
//v4.assign(7, 100);
//for (int i = 0; i < v4.size(); i++)
//{
// cout << v4[i] << " ";
//}
//cout << endl;
//v4.assign(6, 66);
//cout << "v4 size:" << v4.size() << " v4 capacity:" << v4.capacity() << " v4 address:" << &v4 << " v4.data address:" << v4.get_address() << endl;
//for (int i = 0; i <= v4.size(); i++)
//{
// cout << v4[i] << " ";
//}
//cout << endl;
//cout << "at func:"<<v4.at(5)<<endl;
//cout << "back func:" << v4.back() << endl;
//cout << "begin func:" << v4.begin() << endl;
}
测试结果
总结
基本符合预期,功能未全部实现,只是实现了常用的几个。