主要練習模板的使用
**如下:
注意:模板的宣告和實現應該現在乙個檔案中,否則會出現連線錯誤:「無法解析的外部符號 該符號在函式 _main 中被引用」#ifndef _my_vector_h_
#define _my_vector_h_
template
class my_vector
my_vector(const my_vector & rhs)
}const my_vector & operator= (const my_vector & rhs);
t & operator(int index)
int capacity() const
int size() const
void reserve(int newcapacity);
void resize(int newsize);
void push_back(const t & a);
~my_vector()
private:
int m_size; //當前元素個數
int m_capacity; //當前容量
t* p_arrays; //內建陣列
};template
const my_vector& my_vector::operator=(const my_vector& rhs)
return *this;
}}template
void my_vector::reserve(int newcapacity)
m_size = numtocopy;
m_capacity = newcapacity;
delete old_p;
}template
void my_vector::resize(int newsize)
m_size = newsize;
}template
void my_vector::push_back(const t & a)
p_arrays[m_size++] = a;
}#endif
C 模板實現佇列
我準備練習一下模板的知識,然後自己實現vector類。在這之前,先用模板實現乙個佇列來熱身吧。佇列的底層是鍊錶。主要是熟悉一下模板的寫法。另外,就是模板的定義和實現都要寫在乙個檔案中 export關鍵字可以避免這樣。還沒用過 所以倒數第二行我加了個 include queue.hpp 只能是hpp,...
C 模板實現順序表
pragma once include include include linearlist.h using namespace std const int defaultsize 100 templateclass seqlist public linearlist int size const ...
C 利用模板實現佇列
這個 的實現主要使用了類模板以及模板函式,友元,成員模板以及成員模板函式。這裡需要注意的時,類的成員函式在實現的時候必須滿足以下幾點 1 必須以template開始,後接模板形參表 2 必須指出是那個類成員 3 類名必須包含模板形參 具體的格式如下 templatereturn type queue...