C++STL之vector的自定義實現
2018.10.15-至今一直在尋找C++開發崗位的工作,但是剛畢業,沒有工作經驗與項目經歷,工作一直不太好找。聊天中有個朋友說你把STL自己實現一下,放在博客上,簡單的當作自己的項目。於是就寫了這篇博客。
如有用詞不當或者錯誤之處,請聯繫我改正
注意點:
1、如果是像本文一樣採取類模板template(泛型)來實現,則類的實現與類的聲明要放在同一個文件中,否則即使你 #include"xxx.h" 了,在編譯時也會提示“無法解析的外部符號”。
2、vector容器自身涉及到insert與erase,因此賦值操作必不可少,本篇博客是實現時暫未考慮到複雜數據類型,即僅僅支持簡單的char、double等基本數據類型。有機會會單獨發一篇博客來實現支持複雜數據類型(如class、struct)的代碼。
3、目前爲止,自定義容器的常用功能如resize()、reserve()、erase()和insert()的重載均爲實現,後續研究出來會更新的。
4、暫時不給出測試代碼,實際上測試法與vector容器 用法相同,但是應該不支持迭代器(iterator),請謹慎使用begin()、end()等函數的返回值。
5、本文開發環境 VS2013。
類MyVector的聲明
template<class T>
class MyVector{
public:
MyVector<T>();
~MyVector<T>();
int size();
int capacity();
void clear();
bool empty();
void swap(MyVector<T>& v);
T at(int index);
void push_back(T v);
void pop_back();
void two(); //容器自增長
T* begin();
T* end();
T front();
T* erase(T* pos);//刪除指定位置的元素,返回下一個元素的位置
//T* erase(T* start,T* end);//刪除指定區間的元素,返回end對應元素的位置
T* insert(T* pos, T val);//返回val所在的位置
void insert(T* pos, int count, T val);//在pos位置插入count個val
private:
T* data; //數組用於存放元素
int Size; //實際個數
int Capacity;//現有的容量
};
類模板MyVector的外部實現
構造與析構
template<class T>
MyVector<T>::MyVector(){
Size = 0;
Capacity = 10;
data = new T[Capacity];
}
template<class T>
MyVector<T>::~MyVector(){
}
size()、capacity()、empty()、clear()的實現
template<class T>
int MyVector<T>::size(){
return this->Size;
}
template<class T>
int MyVector<T>::capacity(){
return this->Capacity;
}
template<class T>
bool MyVector<T>::empty(){
return (Size == 0);
}
template<class T>
void MyVector<T>::clear(){
while (!empty())
pop_back();
}
at()、push_back()、pop_back()的實現
template<class T>
T MyVector<T>::at(int index){
if (empty() || index > Capacity)//空 下標越界
return (T)NULL;
return data[index];
}
template<class T>
void MyVector<T>::push_back(T v){
//two();
data[Size++] = v;
}
template<class T>
void MyVector<T>::pop_back(){
if (empty())//空不可刪除
return;
//throw runtime_error("empty");
//data[Size - 1] = (T*)NULL;
--Size;
}
容器自增長two()、swap()的實現
template<class T>
void MyVector<T>::two(){
if (size() > capacity()){
Capacity *= 2;
T* old = this->data;
data = new T[Capacity];
for (int i = 0; i < Size; ++i)
data[i] = old[i];
delete[]old;
}
}
template<class T>
void MyVector<T>::swap(MyVector<T>& v){
//考慮交換雙方的元素個數
int len = size();
T* tmp = new T[len];//將自身的數據存儲在tmp中
for (int i = 0; i < len; ++i){
tmp[i] = at(i);
}
clear();
for (int i = 0; i < v.size(); ++i){
push_back(v.at(i));
}
v.clear();
for (int i = 0; i < len; ++i)
v.push_back(tmp[i]);
}
front()、begin()、end()的實現
template<class T>
T MyVector<T>::front(){
if (!empty()){
return data[0];
}
else
return (T)NULL;
}
template<class T>
T* MyVector<T>::begin(){
if (!empty())
return data;
else
return (T*)NULL;
}
template<class T>
T* MyVector<T>::end(){
if (!empty())
return data + Size;
else{
cout << "空" << endl;
return (T*)NULL;
}
}
insert()、erase()的實現
template<class T>
T* MyVector<T>::erase(T* pos){//
T* p=pos;
T* tmp = pos + 1;
while (tmp != end()){
*pos = *tmp;
++pos;
++tmp;
}
--Size;
return p;
}
template<class T>
T* MyVector<T>::insert(T* pos, T val){
if (Size + 1 > Capacity)
two();//容量不夠時,自增長
T* p = end();
int i = 0;
for (i = Size; p != pos; --i, --p){
data[i] = data[i - 1];
}
data[i] = val;
++Size;
return pos;
}
//該函數目前存在bug,當Capacity初始化過小時,會發生“中斷”
template<class T>
void MyVector<T>::insert(T* pos, int count, T val){
Size += count;
while (size() > capacity()){
two();//容量不夠時,自增長
cout << size() << " " << capacity() << endl;
}
T* p = (T*)NULL;
int i = 0,j=0;
for (i = size() - 1; data[i]!=*(pos-1); --i){
data[i+count] = data[i];
}
for (p = pos; p < pos +count; ++p)
*p = val;
}