Kırmızı Şapka

RedHat, Uygulama Kurulumu, Ayarlar vs.

Cuma, Temmuz 03, 2009

Sablonlu Fonksiyonlar (Function Templates)


#include <vector>
#include <iostream>

using namespace std;
/**
* Return the maximum of an array
* Assumes a.size() > 0
* Comparable objecs must provide operator< and operator=
*/

template <typename Comparable>
const Comparable & findMax(vector<Comparable> &a)
{
int maxIndex = 0;
for (int i=0; i < a.size(); i++)
if(a[maxIndex] < a[i] ) //operator < burada gerekli
maxIndex = i;
return a[maxIndex]; //operator= burada kullanilacak

}

int main()
{
vector<int> v1(7);
for (int i=0; i < v1.size(); i++)
v1[i] = i;
cout<< findMax(v1) << endl;
return 0;
}

Etiketler:

Toplam 0 Yorum:

Yorum Gönder

<< Ana Sayfa