2012-05-12 37 views
2

我有一个问题修改C++函数,以便它可以与任何数据类型一起使用。任何和所有的帮助将非常感激。下面是我的代码,还有一个我不断遇到的错误。C++:问题与功能模板

// This program demonstrates the QuickSort Algorithm. 
#include <iostream> 
#include <algorithm> 
using namespace std; 


//************************************************ 
// quickSort uses the quicksort algorithm to  * 
// sort set, from set[start] through set[end]. * 
//************************************************ 

template <class T> 
void quickSort(T set[], int start, int end) 
{ 
    T pivotPoint; 

    if (start < end) 
    { 
     // Get the pivot point. 
     pivotPoint = partition(set, start, end); 
     // Sort the first sub list. 
     quickSort(set, start, pivotPoint - 1); 
     // Sort the second sub list. 
     quickSort(set, pivotPoint + 1, end); 
    } 
} 

//********************************************************** 
// partition selects the value in the middle of the  * 
// array set as the pivot. The list is rearranged so  * 
// all the values less than the pivot are on its left  * 
// and all the values greater than pivot are on its right. * 
//********************************************************** 

template <class T1> 
int partition(T1 set[], int start, int end) 
{ 
    int pivotValue, pivotIndex, mid; 

    mid = (start + end)/2; 
    swap(set[start], set[mid]); 
    pivotIndex = start; 
    pivotValue = set[start]; 
    for (int scan = start + 1; scan <= end; scan++) 
    { 
     if (set[scan] < pivotValue) 
     { 
     pivotIndex++; 
     swap(set[pivotIndex], set[scan]); 
     } 
    } 
    swap(set[start], set[pivotIndex]); 
    return pivotIndex; 
} 


//********************************************** 
// swap simply exchanges the contents of  * 
// value1 and value2.       * 
//********************************************** 

template <class T> 
void swap(T &value1, T &value2) 
{ 
    int temp = value1; 

    value1 = value2; 
    value2 = temp; 
} 

int main() 
{ 
    const int SIZE = 10; // Array size 
    int count;   // Loop counter 

    // need to override the [] function? 
    int array[SIZE] = {7, 3, 9, 2, 0, 1, 8, 4, 6, 5}; 

    // Display the array contents. 
    for (count = 0; count < SIZE; count++) 
     cout << array[count] << " "; 
    cout << endl; 

    // Sort the array. 
    quickSort(array, 0, SIZE - 1); 

    // Display the array contents. 
    for (count = 0; count < SIZE; count++) 
     cout << array[count] << " "; 
    cout << endl; 
    return 0; 
} 

对于该quicksort()函数中读取pivotPoint = partition(set, start, end);行了,我收到此错误:

main.cpp:24: error: no matching function for call to 'partition(int*&, int&, int&)' 

如果有人可以让我知道这意味着什么,以及如何适应它,我会很欣赏。

+8

有已经算法['性病:: partition'(http://en.cppreference.com/w/CPP /算法/分区)。停止“滥用命名空间标准;”并自己拼写出来。 –

+0

感谢您的快速回复@KerrekSB。不过,我不确定自己完全明白你在说什么。 –

+0

你需要考虑你的函数被调用的顺序。 'quickSort'在定义之前调用'partition'函数,'partition'在定义之前调用'swap'函数。另外,'pivotPoint'类型不应该是'T',而是'int'在你的情况下。如果这不是家庭作业,那么使用'std :: swap','std :: partition'(甚至是'std :: sort')来查看。 – Chad

回答

4

partition函数在使用之前需要声明。

移动partition定义quicksort定义之前或者在那个地方添加声明:

template <class T1> 
int partition(T1 set[], int start, int end); 
+0

明白了!谢谢! –