Frage

I'm working on a programming assignment, to make a function template that can handle ints and doubles. I've done that, but for fun, I wanted to make it able to handle strings, as well. Here is the function below. How would I go about making it handle strings?

// This program demonstrates the QuickSort Algorithm.
#include <iostream>
#include <algorithm>
#include <ctype.h> //needed for string handling?
using namespace std;



//**********************************************************
// 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 T>
int partition(T set[], int start, int end)
{
   int pivotValue, pivotIndex, mid;

   mid = (start + end) / 2;
   swap(set[start], set[mid]);
   pivotIndex = start;
   pivotValue = set[start]; // main.cpp:28: error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'int' in assignment
   for (int scan = start + 1; scan <= end; scan++)
   {
      if (set[scan] < pivotValue) // main.cpp:31: error: no match for 'operator<' in '*(((std::string*)(((long unsigned int)scan) * 8ul)) + set) < pivotValue'
      {
         pivotIndex++;
         swap(set[pivotIndex], set[scan]);
      }
   }
   swap(set[start], set[pivotIndex]);
   return pivotIndex;
}

//************************************************
// 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); // main.cpp:56: error: no match for 'operator-' in 'pivotPoint - 1'
      // Sort the second sub list.
      quickSort(set, pivotPoint + 1, end); // main.cpp:58: error: no match for 'operator+' in 'pivotPoint + 1'
   }
}

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

   // create arrays of various data types
   int array[SIZE] = {7, 3, 9, 2, 0, 1, 8, 4, 6, 5};
//   string array[SIZE] = {"7", "3", "9", "2","7", "3", "9", "2","a","r"};
   double array2[SIZE] = {7.1, 3.3, 9.0, 2.7, 0.2, 1.5, 8.9, 4.5, 6.9, 5.45};

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

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

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

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

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

   // Display the int array contents.
   cout << "Displaying the double array after sorting" << endl;
   for (count = 0; count < SIZE; count++)
      cout << array2[count] << " ";
   cout << endl;

   return 0;
}

Thanks in advance,

Adam

War es hilfreich?

Lösung

If you use std::string for T, you're probably pretty close to working already.

If you use char*, you'll need to supply a comparison functor as a template parameter (or have some other way of specifying the comparison method for T, like a type-trait class).

Also, you shouldn't implement your own swap. std::swap already exists and will do smart things for certain types (e.g. swapping two vectors is constant-time instead of copying every object in the vector).

Andere Tipps

Works in MSVC, which is a bit permissive so let me know if you run into problems with your compiler.

This solution uses functors (a class/struct with operator()) which means the object can be called as if it were a function. It also uses template specialization - take a look at what happens if you remove the template < > versions of LessThanCompare - char* will fall back to comparing the pointer value (giving random results).

In a better implementation you would use a class to put your quicksort and pivot functions - then you could use default templates and avoid having to call like quickSort<char*, LessThanCompare<char*> > - you could just say quicksort but that's getting a bit beyond the scope of the question!

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

template <class T>
struct LessThanCompare
{
    bool operator()(T lhs, T rhs)
    {
        return lhs < rhs;
    }
};


template < >
struct LessThanCompare<char*>
{
    bool operator()(char* lhs, char* rhs)
    {
        return strcmp(lhs, rhs) == -1;  // Note strcmp returns -1 if lhs < rhs
    }
};

template <class T, class Comparator>
int partition(T set[], int start, int end)
{
    Comparator CompareLessThan; // Declare an instance of the Comparator

   T pivotValue; 
   int pivotIndex, mid;  // Is mid an index or a value - use descriptive names!

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

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

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

   if (start < end)
   {
      // Get the pivot point.
      pivotPoint = partition<T, Comparator >(set, start, end);
      // Sort the first sub list.
      quickSort<T, Comparator>(set, start, pivotPoint - 1); // main.cpp:56: error: no match for 'operator-' in 'pivotPoint - 1'
      // Sort the second sub list.
      quickSort<T, Comparator>(set, pivotPoint + 1, end); // main.cpp:58: error: no match for 'operator+' in 'pivotPoint + 1'
   }
}

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

   // Create arrays of strings
   char* cstrArr[SIZE] = {
       "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

   std::string strArr[SIZE];
   for (int i = 0; i < SIZE; ++i)
   {
       strArr[i] = std::string(cstrArr[i]);
   }

   quickSort<char*, LessThanCompare<char*> >(cstrArr, 0, SIZE-1);
   quickSort<std::string, LessThanCompare<std::string> >(strArr, 0, SIZE-1);

   for (int i = 0; i < SIZE; ++i)
   {
       cout << cstrArr[i] << "\t\t" << strArr[i] << '\n';
   }

   return 0;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top