How to sort with a lambda?

C++SortingLambdaCharConstants

C++ Problem Overview


sort(mMyClassVector.begin(), mMyClassVector.end(), 
	[](const MyClass & a, const MyClass & b)
{ 
	return a.mProperty > b.mProperty; 
});

I'd like to use a lambda function to sort custom classes in place of binding an instance method. However, the code above yields the error:

> error C2564: 'const char *' : a function-style conversion to a built-in type can only take one argument

It works fine with boost::bind(&MyApp::myMethod, this, _1, _2).

C++ Solutions


Solution 1 - C++

Got it.

sort(mMyClassVector.begin(), mMyClassVector.end(), 
    [](const MyClass & a, const MyClass & b) -> bool
{ 
    return a.mProperty > b.mProperty; 
});

I assumed it'd figure out that the > operator returned a bool (per documentation). But apparently it is not so.

Solution 2 - C++

You can use it like this:

#include<array>
#include<functional>
using namespace std;
int main()
{
    array<int, 10> arr = { 1,2,3,4,5,6,7,8,9 };

    sort(begin(arr), 
         end(arr), 
         [](int a, int b) {return a > b; });

    for (auto item : arr)
  	  cout << item << " ";

    return 0;
}

Solution 3 - C++

Can the problem be with the "a.mProperty > b.mProperty" line? I've gotten the following code to work:

#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
#include <sstream>

struct Foo
{
    Foo() : _i(0) {};

    int _i;

    friend std::ostream& operator<<(std::ostream& os, const Foo& f)
    {
        os << f._i;
        return os;
    };
};

typedef std::vector<Foo> VectorT;

std::string toString(const VectorT& v)
{
    std::stringstream ss;
    std::copy(v.begin(), v.end(), std::ostream_iterator<Foo>(ss, ", "));
    return ss.str();
};

int main()
{

    VectorT v(10);
    std::for_each(v.begin(), v.end(),
            [](Foo& f)
            {
                f._i = rand() % 100;
            });

    std::cout << "before sort: " << toString(v) << "\n";

    sort(v.begin(), v.end(),
            [](const Foo& a, const Foo& b)
            {
                return a._i > b._i;
            });

    std::cout << "after sort:  " << toString(v) << "\n";
    return 1;
};

The output is:

before sort: 83, 86, 77, 15, 93, 35, 86, 92, 49, 21,
after sort:  93, 92, 86, 86, 83, 77, 49, 35, 21, 15,

Solution 4 - C++

You can sort an array like this:

#include <bits/stdc++.h>
using namespace std;
int main() {
    int q[] = {1, 3, 5, 7, 9, 2, 4, 6, 8 ,10};
    sort(q, q + 10, [&](int A, int B) { return A < B; });
    for (int i = 0; i < 10; i++)
        cout << q[i] << ' ';
    return 0;
}
before sort: 1 3 5 7 9 2 4 6 8 10
after sort: 1 2 3 4 5 6 7 8 9 10 

I'd always like to use lambda to sort a array of struct in acm contests like this:

struct item {
    int a, b;
};

vector<item> q;

sort(q.begin(), q.end(), [&](item t1, item t2) {
    return t1.a < t2.a;
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBTRView Question on Stackoverflow
Solution 1 - C++BTRView Answer on Stackoverflow
Solution 2 - C++AdrianView Answer on Stackoverflow
Solution 3 - C++StephanView Answer on Stackoverflow
Solution 4 - C++HeyyaView Answer on Stackoverflow