c++ - 使用 Boost lambda 和绑定(bind)排序时出错

标签 c++ sorting boost lambda bind

我正在尝试根据其属性之一对自定义对象的 vector 进行排序,但出现以下错误:

/usr/include/boost/lambda/detail/function_adaptors.hpp:264:15: error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ make: * [src/boost_lambda.o] Error 1

知道这个错误吗?你可以在这里找到代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

struct Parent{
    int getAge(){
    return age;
}
int age;
std::string name;
};

int main()
{
    std::vector<Parent> aListParents;

    Parent aParent1;
    aParent1.age=1;
    aParent1.name="parent1";
    Parent aParent2;
    aParent2.age=2;
    aParent2.name="parent2";

    aListParents.push_back(aParent1);
    aListParents.push_back(aParent2);

    std::sort(aListParents.begin(), aListParents.end(),
        bind(&Parent::age, boost::lambda::_1)  < bind(&Parent::age, boost::lambda::_2));
}

最佳答案

适用于:

编译器版本:http://rextester.com/

博斯特版本:

  • 1.55.0 用于 vc++
  • 1.54.0 for clang
  • 1.54.0 用于 gcc

关于c++ - 使用 Boost lambda 和绑定(bind)排序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23455253/

相关文章:

c++ - 访问 UBLAS 稀疏 vector 中非零元素的索引

c++ - 访问同一源文件中另一个 .cpp 的成员函数?

c++ - 是否有可能获得未知类的类成员的数量和类型?

performance - 在Elasticsearch中排序

python - 如何在 Pandas 中按降序对两列进行排序?

c++ - boost::asio::io_service::在多个线程中运行

c++ - 将右值引用传递给 boost::in_place 函数

c++ - seekp() 语句似乎是不必要的,但实际上并非如此

c++ - Catch2 - undefined reference

c# - 如何使用 LINQ 按深度级别对对象层次结构进行排序?