While writing code for a mergesort in C++ as an exercise I came to the realization that I didn't really have a reliable way to declare an array that doesn't have a specific type at compile time.
In Java, there is a way to make an array Comparable to facilitate sorting with operators (<. >, =). What I would like to know is: Does C++ contain any ways to make arrays comparable? And is there any way I can create an array before I know what type of array it is?
For example, I was given code (In Java) for a simple mergesort program
public static void merge(Comparable[] a, int lo, int mid, int hi){
int i = lo, j = mid+1;
for(int k = lo; k <= hi; k++)
aux[k] = a[k];
for(int k = lo; k <= hi; k++)
if (i > mid) a[k] = aux[j++];
else if (j > high) a[k] = aux[i++];
else if (less(aux[j], aux[i])) a[k] = aux[j++];
else a[k] = aux[i++];
}
If I was creating a class called mergesort, how would I implement this in C++ while also making it possible to determine array type after the program has run?
std::sort.std::vector. C-style arrays don't play nicely, generally you should avoid using them. Of course, quantdev's suggestion is the best solution to your problem as it does not constrain your code to only be used with one type of container.