Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 - binding sort function

Tags:

c++

c++11

I'd like to save myself some typing and therefore define something like this:

using namespace std;

vector<MyClass> vec;

auto vecsort = bind(sort, vec.begin(), vec.end(), [] (MyClass const &a, MyClass const &b) {
        // custom comparison function
    });

vecsort(); // I want to use vecsort() a lot afterwards

For some reason this doesn't compile - why?

Using boost is not an option.

Minimal working example:

#include <vector>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

int main() {

    vector<pair<int, int>> vec;
    for (int i = 0; i < 10; i++)
        vec.push_back(make_pair(10 - i, 0));

    auto vecsort = bind(sort, vec.begin(), vec.end(), [] (pair<int, int> const &a, pair<int, int> const &b) {
            return a.first < b.first;
        });

    vecsort();

}

Error:

error: no matching function for call to 'bind(<unresolved overloaded function type>, std::vector<std::pair<int, int> >::iterator, std::vector<std::pair<int, int> >::iterator, main()::__lambda0)'

like image 600
ryyst Avatar asked Dec 12 '13 17:12

ryyst


People also ask

What is C++ sort function?

What is Sort Function in C++? Sort is an in-built function in a C++ STL ( Standard Template Library). This function is used to sort the elements in the range in ascending or descending order.

Why does the std::sort receive a function?

std::sort() is a built-in function in C++'s Standard Template Library. The function takes in a beginning iterator, an ending iterator, and (by default) sorts the iterable in ascending order. The function can also​ be used for custom sorting by passing in a comparator function that returns a boolean.

How do you use the sort function?

The SORT function sorts the contents of a range or array. In this example, we're sorting by Region, Sales Rep, and Product individually with =SORT(A2:A17), copied across cells F2, H2, and J2.

How do you sort a std vector?

Sorting a vector in C++ can be done by using std::sort(). It is defined in<algorithm> header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintains the relative order of equal elements.


2 Answers

The problem is that std::sort isn't a function object. It is a function template. The easiest way to deal with the issue is to create a simple wrapper object:

struct sorter {
    template <typename RndIt, typename Cmp>
    void operator()(RndIt begin, RndIt end, Cmp cmp) {
        std::sort(begin, end, cmp);
    }
};

Now you can use

std::bind(sorter(), vec.begin(), vec.end(), [](...){ ... });
like image 154
Dietmar Kühl Avatar answered Oct 07 '22 21:10

Dietmar Kühl


Others have mentioned why it doesn't compile, but does this alternate solution work for you? This uses another lambda, instead of bind, to create the std::function.

#include <vector>
#include <utility>
#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main() {

    vector<pair<int, int>> vec;
    for (int i = 0; i < 10; i++) {
        vec.push_back(make_pair(10 - i, 0));
    }

     auto vecsort = [&vec] {
        sort(vec.begin(), vec.end(), 
        [] (pair<int, int> const &a, pair<int, int> const &b) {
            return a.first < b.first;
        });
     };

    // vecsort will work as long as vec is in scope.
    // vecsort will modify the original vector.
    vecsort();
    for (auto i : vec) {
        std::cout << '(' << i.first << ", " << i.second << ") ";
    }
    std::cout << endl;

    vec.push_back(make_pair(-42, 0));
    vecsort();
    for (auto i : vec) {
        std::cout << '(' << i.first << ", " << i.second << ") ";
    }
    std::cout << endl;
}

Output:

(1, 0) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 0) (8, 0) (9, 0) (10, 0)
(-42, 0) (1, 0) (2, 0) (3, 0) (4, 0) (5, 0) (6, 0) (7, 0) (8, 0) (9, 0) (10, 0)

See it run here: http://ideone.com/W2YQKW

like image 43
James Avatar answered Oct 07 '22 21:10

James