为什么leetcode c ++ sort()给出编译错误?

时间:2019-06-20 03:32:36

标签: c++ sorting cmp

我正在执行leetcode406。按高度进行队列重建。我想先对vector进行排序,但是当我完成sort和cmp部分并运行代码时,它给了我编译错误。

class User(models.Model):
    recipes = models.ManyToManyField(Recipe, through='UserRecipes')



class UserRecipies(models.Model):
     recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, related_name='user_recipes')
     user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='recipes_per_user')
     created_at = models.DateTimeField(auto_now_add=True)

     class Meta:
        ordering = ['created_at',]

我应该怎么做才能使其正常工作?

solution.cpp: In member function reconstructQueue
Line 12: Char 47: error: invalid use of non-static member function 'bool Solution::cmp(std::pair<int, int>, std::pair<int, int>)'
         sort(people.begin(), people.end(), cmp);
                                               ^

1 个答案:

答案 0 :(得分:1)

std::sort需要具有此签名的比较功能:

bool cmp(const Type1 &a, const Type2 &b);

,您传递的是成员函数,该函数不相同。 解决方案:传递lambda,如果您需要在进行比较时访问该类的成员,请通过引用捕获外部世界。

sort(...,[&](const vector<int>& v1,const vector<int>& v2) -> bool { ... });

第二,sort函数需要传递两个向量,它不能自动转换为std::pair

相关问题