1

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?

5
  • Most of this is done using the Standard Library where you write a comparator template function for use on one of the containers. See: std::sort. Commented Jun 4, 2014 at 21:48
  • 2
    Are you looking for a template function?!? Commented Jun 4, 2014 at 21:50
  • I don't see why you need "arrays comparable" for this - you just need to have the element type be comparable. Not the array as a whole. Commented Jun 4, 2014 at 21:55
  • 1
    Just a nitpick: you should be more careful with the wording, you don't want to make arrays comparable, but to compare the elements in the array, you want to have a generic type in an array while at the same time ensuring that it is comparable. At any rate the comment above has the answer: you are looking for templates Commented Jun 4, 2014 at 21:55
  • note, the C++ equivalent of Java arrays is 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. Commented Jun 4, 2014 at 21:57

1 Answer 1

6

The STL compliant way of implementing the same thing (an algorithm over a sequence), is to make your MergeSort function a function template taking a pair of iterators as a parameter, not an array.

See the declaration of std::sort as an example, :

template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);

As you can see nowhere in this declaration is the type of the sequence elements explicit.

And optionally pass a comparator (that would itself takes two of your objects of different types as parameters) :

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

EDIT :

The nice thing of doing that, is that your merge sort will work on any STL container (vector, list, queue, string, array, etc...) and you will be able to compose it with any other STL algorithm.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This helps a bunch! I've heard about template (I'm still a novice programmer) so I'll be sure to read more into this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.