C++ 如何用 lambda 排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5122804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:26:59  来源:igfitidea点击:

How to sort with a lambda?

c++sortinglambdacharconst

提问by BTR

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:

我想使用 lambda 函数对自定义类进行排序,而不是绑定实例方法。但是,上面的代码产生了错误:

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

错误 C2564:“const char *”:到内置类型的函数式转换只能采用一个参数

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

它与boost::bind(&MyApp::myMethod, this, _1, _2).

回答by BTR

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.

我认为它会发现 > 运算符返回了一个布尔值(每个文档)。但显然并非如此。

回答by Adrian

To much code, you can use it like this:

对于很多代码,您可以像这样使用它:

#include<array>
#include<functional>

int main()
{
    std::array<int, 10> vec = { 1,2,3,4,5,6,7,8,9 };
    std::sort(std::begin(vec ), std::end(vec ), [](int a, int b) {return a > b; });
    for (auto item : vec)
      std::cout << item << " ";

    return 0;
}

Replace "vec" with your class and that's it.

用你的班级替换“vec”,就是这样。

回答by Stephan

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

问题可能出在“a.mProperty > b.mProperty”行上吗?我已经得到以下代码工作:

#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,