模板實現順序表和雙向鍊錶

2021-08-19 23:17:52 字數 2033 閱讀 4273

模板詳解可以點這裡

#include 

#include

#include

using

namespace

std;

//區別下面兩種概念

//類的型別: vector

//類: vector

template

class vector

~vector()//析構函式

//v2(v1)

vector(vector& v)//拷貝建構函式

delete _first;

_first = tmp;

_finish = _first + v.size();

_capacity = _first + v.capacity();}}

//v2 = v1

vector& operator = ( vector&v)//拷貝賦值函式

return *this;

}void swap(vector&s)

void pushback(const t& x)

void popback()

void pushfront(const t& x)

void popfront()

void expand(size_t n)//擴容函式

else

delete _first;

_first = tmp;

_finish = _first + size;

_capacity = _first + n;}}

void insert(size_t pos,const t&x)

t* end = _finish - 1;

while (end > _first + pos - 1)//從插入的位置元素向後移動

*(_first + pos) = x;

++_finish;

}void erase(size_t pos)

--_finish;

}size_t find(const t& x)

}return -1;

}t& operator(size_t pos)//修改某個位置

const t& operator(size_t pos) const

size_t size()

size_t capacity()

bool empty()

else

}void show()

cout

<< endl;

}};

#include

using namespace std;

template

struct listnode

;template

class list

~list()//析構函式

void clean()//釋放插入的節點

_head->_next = _head;

_head->_prev = _head;

}//l1(l2)

list(const list

& l)//拷貝建構函式

:_head(new node)

}list

& operator = (const list

& l)//拷貝賦值函式

return

*this;

}void pushback(t x)

void popback()

void pushfront(t x)

void popfront()

node* find(t x)

cur = cur->_next;

}return

null;

}void insert(node* pos, t x)

void erase(node* pos)

void show()

cout << endl;

}bool empty()

else

}};

C 模板實現順序表和煉表

順序表 pragma once include include include using namespace std 模板實現順序表 考慮深層次的深淺拷貝問題 template class seqlist t operator size t pos void print private t a s...

C 模板實現Vector和雙向鍊錶

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...

模板實現雙向鍊錶基本操作

1 template為小寫 2 模板程式設計用的不熟練,尤其是表現在模板符號格式的應用上 3 如果引數為物件,用引用格式,並考慮是否為常引用 ifndef test hpp define test hpp templateclass dllnode dllnode const t i,dllnode...