Overloading algorithms

Hi,

Is there a way to overload std library algorithms such as sort() etc?
I would like to be able to sort my own class in a specific way.
I would like to be able to sort my own class in a specific way.

I don't know if you can, but in my opinion you should not overload the functions from the standard library.

If you overload a function you create a function that has the exact same name (and different arguments). Why would you like to call your sort function std::sort?
Suppose you remove every "using namespace std;" in your code and instead place "std::" in front of all the functions that you use from the standard library.

Now if you create a function that is called sort() yourself, the compiler will know which one to use, because yours is called ::sort (or <your class name>::sort) and the one from the std-lib is called std::sort().

This is also why people often say it is not a good habit to use "using namespace std;", it increases the chance that you create a function or keyword with a name that is already in use and that might result in other output than you were expecting.

Have a look here: http://www.cplusplus.com/doc/tutorial/namespaces/
Last edited on
Ok I see, so there is no way to use a standard library function with user defined types?
Use the overload of std::sort() which accepts a third argument (a binary predicate which specifies how objects are compared).
Overload (3) here: http://en.cppreference.com/w/cpp/algorithm/sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>

int main()
{
    struct my_class { int x ; int y ; };

    // custom predicate to compare two objects of type my_object
    // return true if object a should appear before object b in the sorted sequence
    const auto cmp_my_class = [] ( my_class a, my_class b ) { return (a.x+a.y) < (b.x+b.y) ; } ;

    my_class my_objects[] { {1,8}, {4,8}, {2,5}, {5,9}, {1,9}, {2,7}, {5,6}, {2,9} } ;

    //  use custom predicate to compare objects for sorting
    std::sort( std::begin(my_objects), std::end(my_objects), cmp_my_class ) ;

    for( const auto& object : my_objects ) std::cout << '{' << object.x << ',' << object.y << "}  " ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/34fd906c8cd6d3bc
Usually there is a way, but for that you don't have to overload it.

Have a look at:
http://www.cplusplus.com/reference/algorithm/sort/

This describes std::sort. As you can see this function in the std library is already overloaded but that is not very important now. What is important is that the function can be used for everything that has a first and a last "RandomAccessIterator". In other words, of you can put your objects in for example a std::vector, you can give them to the std::sort function.

The overloading is to enable you to provide your own comparison rule. You could for example use std::sort to sort "20 < values < 50", to do that in one run you would need to provide a Compare function that says that every value larger than 20 and smaller than 50 is larger than any other value.
For numbers this is obviously weird and that is why you can use the function without providing a comparison function for for example numbers and strings.
But it becomes more logical if you decide to sort "cars" or "user defined objects".

std::sort can be used to sort anything that is provided in a "RandomAccessIterator", for as long as you tell it what you consider larger and smaller.
Topic archived. No new replies allowed.