小编Mal*_*Liu的帖子

在std :: sort和std :: priority_queue中如何使用自定义比较函数的困惑

我试图弄清楚为什么您为std::sort和定义了不同的自定义比较功能的原因std::priority_queue

例如,因为std::sort我可以做这样的事情:

bool compare(const vector<int>& a, const vector<int>& b)
{
    return a[0] < b[0];
}
class foo
{
public:
    vector<vector<int>> f(vector<vector<int>> list)
    {
        std::sort(list.begin(), list.end(), compare);
        return list;
    }
};


int main()
{
    vector<vector<int>> t = { {2,1},{1,0},{3,7} };
    foo n;

    auto ans = n.f(t);
    for (vector<int> x : ans)
    {
        printf("x[0]: %d , x[1]: %d \n", x[0], x[1]);
    }
    return 0;
}

Run Code Online (Sandbox Code Playgroud)

运行代码后,结果为:

x [0]:1,x [1]:0

x [0]:2,x [1]:1

x [0]:3,x [1]:7

但是,如果我这样定义另一个函数 …

c++ templates stl

3
推荐指数
1
解决办法
65
查看次数

标签 统计

c++ ×1

stl ×1

templates ×1