gpt4 book ai didi

c++ - 错误 C2893 : Failed to specialize function template 'unknown-type std::less::operator ()(_Ty1 &&,_Ty2 &&) const'

转载 作者:行者123 更新时间:2023-11-30 01:53:14 26 4
gpt4 key购买 nike

我写了一个函数,它将接受一个点 vector ,点是一个结构,并将使用稳定排序对其进行排序,我得到以下错误:

    Error   1   error C2893: Failed to specialize function template 'unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const'

这就是我的程序应该做的:

    in:
5 8
3 6
10 9
8 11
7 4

和显示:

    out:
3 6
5 8
7 4
8 11
10 9

这是我的代码:

    #include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct points
{
int a, b;

};

int main()
{

int nbrDeLignes = 0;
cin >> nbrDeLignes;

vector<points> tab(nbrDeLignes);
for (int i = 0; i < nbrDeLignes; i++)
{
points p;

cin >> p.a >> p.b;
tab.push_back(p);
}

//stable_sort(tab.begin(), tab.end());

for (const points &point : tab)
{
cout << point.a << " " << point.b << endl;
}

return 0;
}

任何帮助请;

最佳答案

stable_sort不知道如何对您的 struct 进行排序因为没有为它定义比较运算符。你要么需要制作 points一个class并覆盖 <运营商,或提供 stable_sort具有比较功能,例如。

bool compare_points(point p1, point p2)
{
return p1.a < p2.a;
}

stable_sort(tab.begin(), tab.end(), compare_points);

关于c++ - 错误 C2893 : Failed to specialize function template 'unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23305726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com