template
: : (){ . . . }
Sinf obyektlari bilan ishlash uchun vector qо‘shimcha yuklangan shablon sinfi: 91
{ T *data; int size; public: array(int k) {size =k; data = new T[size]}; T& operator[](int i){return data[i];} ~array () { delete []data; } void input_ vector (); void show_ vector (); };
template void vector :: input_array() { for (int i = 0; i < index; i++) {cin>>data[i] ; cout << ‘ ‘;} } template void vector :: show_array() { for (int i = 0; i < index; i++) cout << data[i] << ‘ ‘; }
Shablon sinflarini qо‘llanilishi shablon funksiyasini a’zosini ifodalaydi. Parametrlari shablon sinflarini ifodalovchi global shablon funksiyalar - algoritmlarini aniqlash mumkin. Masalan oddiy shablonni pufaksimon algoritm orqali tartiblashni shunday aniqlash mumkin.
template void bubble_sort(Vector& v) { unsigned n = v.size(); for (int i=0; i for (int j=n-1; i if (v[j] < v[j-1]) { // v[j] va v[j-1] о‘rnini almashtiramiz T temp = v[j]; v[j] = v[j-1]; v[j-1] = temp; } }
Vector tipini qismiga tenglik funksiyasini berish mumkin emas, balki unga ikkinchi parametr sort() funksiyasini berish kerak. Bu parametr obyekt sinfini ifodalaydi, qaysiki tenglik operatsiyasini qayta aniqlashda.
template void bubble_sort(Vector& v, Compare & cmp) { unsigned n = v.size(); for (int i = 0; i
Do'stlaringiz bilan baham: |