#define _crt_secure_no_warnings 1
#include
#include
using
namespace
std;
template
class vector
vector(size_t n, const t& data)
}vector(const vector& v)
:_pdata(new t[strlen(v._pdata)+1])
,_size(v._size)
,_capacity(v._capacity)
vector& operator =(vector v)
~vector()
}void swap(vector& v)
void pushback(const t& data)
void popback()
void pushfront(const t& data)
_pdata[0] = data;
_size++;
}void popfront()
_size--;
}void insert(size_t pos, const t& data)
_pdata[pos] = data;
_size++;
}void erase(size_t pos)
_size--;
}void assign(size_t n, const t& data)
}void clear()
t& operator (size_t index)
const t& operator (size_t index)const
t& front()
const t& front()const
t& back()
const t& back()const
size_t size()const
size_t capacity()const
bool empty()const
void resize(size_t sz, const t& data = t())
else
if(_size < sz)}}
void reserve(size_t n)
_size = tmp;
}void print()const
cout
cout
<
cout
cout
<
void check_capacity()
delete _pdata;
_pdata = newpdata;
_capacity = _size*2;
2.淺拷貝,自定義型別設計資源時,可能會出錯,思考
(頭節點不儲存資料,不記作有效節點)
#include
#include
using namespace std;
template
t>
struct
node
public:
node
* _next;
node
* _pre;
t _data;
};template
t>
class
list
list
(size_t
n, const
t& data)
:_phead(new
node
)//頭節點(沒有有效data)
c 模板實現vector和list
h檔案 pragma once include include includeusing namespace std templateclass vector 拷貝構造 vector const vector v size v.size capacity v.capacity 賦值運算子的過載 ve...
C 模板實現vector和list
模板分為模板函式和模板類。模板是為了實現泛型程式設計,所謂泛型程式設計,就是指編寫與型別無關的 比如以下的場景 我們想在同乙份 裡實現交換整型,浮點型與字元型。void swap int x,int y void swap double x,double y void swap char x,cha...
模板實現順序表和雙向鍊錶
模板詳解可以點這裡 include include include using namespace std 區別下面兩種概念 類的型別 vector 類 vector template class vector vector 析構函式 v2 v1 vector vector v 拷貝建構函式 del...