簡體   English   中英

模板函數實例化文件?

[英]Template function instantiation file?

我之前已經為類創建了實例化文件,但是對於如何為一個函數創建一個實例,我感到好奇。 這是我制作的程序的示例。

template <typename T>
void deSelSort(T arr[], int n)
{
int j = n - 1;

for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;

for(int i = 0; i < j; i++, j--) {

    int min=i;
    int max=i;
    int temp;

    for (int temp2 = i + 1; temp2 <= j; temp2++)
    {
        if (arr[temp2] < arr[min]) 
            { 
                min = temp2; 
            }
        if (arr[temp2] > arr[max]) 
            { 
                max = temp2; 
            }
    }

    if (min > max) { 
        temp=min; 
        min=max; 
        max=temp;
        arr[min] = max; 
        arr[max] = min;
    }
    if (min > i) 
    { 
        temp = arr[i]; 
        arr[i] = arr[min];
        arr[min] = temp; 
    }
    if (max < j) 
    { 
        temp = arr[j]; 
        arr[j] = arr[max];
        arr[max] = temp; 
    }

    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;


};
}

如果我想創建一個包含整數和雙精度數的數組,實例化文件將是什么樣? 現在我的檔案看起來像這樣

#include "ex3_16.cpp"

template class deSelSort<int>;
template class deSelSort<double>;

如果我想創建一個包含intsdoubles數的數組,實例化文件將是什么樣?

template void deSelSort<int>(int[], int);
template void deSelSort<double>(double[], int);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM