#define _crt_secure_no_warnings
#include using namespace std;
#include #define length 10
typedef int datatype;
class vector
// 建構函式--有size個值為data的元素
vector(size_t n, const datatype& data) }
//拷貝建構函式
vector(const vector& v)
: _capacity(v._capacity)
, _size(v._size)
//析構函式
~vector()
// = 的過載函式
vector& operator=(const vector& v)
return *this;
} //尾插函式
void pushback(const datatype& data)
else
tmp[i] = data;
_size = i;
_pdata = tmp;
delete tmp;
tmp = null;
} }//尾刪函式
void popback()
else
}} //插入指定位置、指定資料函式
void insert(size_t pos, const datatype& data) //注意下標和_size的
_pdata = tmp;
delete tmp;
tmp = null;
} size_t tmp = _size;
for (tmp; tmp >= pos; tmp--)
_pdata[pos - 1] = data;
_size++;
} //刪除指定節點函式
void erase(size_t pos)
_size--;
} }//查詢函式
int find(const datatype& data)const
//注意下標和_size的關係
}return idx + 1;}//清空函式void clear()//求長度函式size_t size()const//給當前順序表上加上size個data資料void resize(size_t size,
const datatype& data = datatype())}//求最大容量函式size_t capacity()const//判空函式bool empty()const//返回第乙個節點的資料datatype&
front()}const datatype& front()const}//返回最後乙個節點的資料datatype& back()}const datatype& back()const}//置數函式void
assign(size_t n, const datatype& data = datatype())_size = idx;}//的過載函式datatype& operator(size_t index)}const datatype& operator(size_t index)const}datatype& at(size_t index)}const datatype& at(size_t index)const}private://判斷容量是否已滿bool _checkcapacity()elsereturn
false;}// 《的過載函式friend std::ostream& operator<<(std::ostream& _cout, const vector& v);private:datatype* _pdata;size_t _capacity;size_t _size;};std::ostream& operator<<(std::ostream& os, const vector& v)return os;}int main(){vector v1(5,1);v1[1] = 2;v1[2] = 3;v1[3] = 4;v1[4] = 5;vector v2;v2 = v1;vector v3(v1);cout << "v1 = " << v1 << endl;cout << "v2 = " << v2 << endl;cout << "v3 = " << v3 << endl;
//以下都是對上述**邏輯的實驗:
v1.pushback(6);
cout << "v1尾插6" << endl;
cout << "v1 = " << v1 << endl;
v2.popback();
cout << "v2尾刪乙個數" << endl;
cout << "v2 = " << v2 << endl;
cout << "v2在第二個位置插上資料『0』"<< endl;
v2.insert(2, 0);
cout << "v2 = " << v2 << endl;
cout << "v2刪除第三個位置上的資料『2』" << endl;
v2.erase(3);
cout << "v2 = " << v2 << endl;
cout << "在v1中查詢資料『2』的位置並返回" << endl;
v1.find(2);
cout << "v1中資料『2』的位置位為:" << v1.find(2) << endl;
cout << "清空v3的資料" << endl;
v3.clear();
cout << "v3 = " << v3 << endl;
cout << "v1的長度為:" << v1.size() << endl;
v2.resize(3, 6);
cout << "給v2上加 3 個 6 " << endl;
cout << "v2 = " << v2 << endl;
cout << "v1的最大容量:" << v1.capacity() << endl;
cout << "返回v1的第乙個資料:" << v1.front() << endl;
cout << "返回v1的最後乙個資料:" << v1.back() << endl;
system("pause");
return 0;
順序表 C 類模板實現
include using namespace std define ok 1 define error 0 template class linklist int initlinklist linklist t l,int maxlistsize 100 初始化大小為100 int getleng...
順序錶類模板
例6.3 順序錶類模板。include include using namespace std template class seqlist 初始化為空表 int length const 計算表長度 int find t x const 尋找x在表中位置 下標 bool isin t x 判斷x是...
順序錶類模板
include include using namespace std template class seqlist 初始化為空表 int length const 計算表長度 int find t x const 尋找x在表中位置 下標 bool isin t x 判斷x是否在表中 bool in...