错误C2893:未能专门化函数模板的未知类型std:: ::operator ()(_Ty1 &&,_Ty2 &&) const'

时间:2020-12-22 16:33:18

i wrote a function that will take a vector of points, points is a structure and will sort it using stable sort, i get the following errors:

我写了一个函数,它取一个点的向量,点是一个结构,它会用稳定的排序来排序,我得到了以下错误:

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

that's what my program should do:

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

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

and display:

和显示:

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

here is my code:

这是我的代码:

    #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;
}

any HELP please ;

任何帮助请;

2 个解决方案

#1


3  

stable_sort doesn't know how to sort your struct because there is no comparison operator defined for it. You either need to make points a class and override the < operator, or provide stable_sort with a comparison function, eg.

stable_sort不知道如何对结构进行排序,因为没有为其定义的比较运算符。您要么需要创建一个类并覆盖 <操作符,或者提供一个具有比较函数的stable_sort(例如)。< p>

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

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

#2


1  

Well, how do you want to order your points? How do you know if a point should come before another point when sorted? Do you order by x coordinate, or what? You need to tell the compiler this!

那么,你想要点什么呢?你怎么知道一个点是否应该在另一个点之前排序?你点的是x坐标还是什么?你需要告诉编译器这个!

In other words, you need to provide an operator<.

换句话说,您需要提供一个操作符<。

#1


3  

stable_sort doesn't know how to sort your struct because there is no comparison operator defined for it. You either need to make points a class and override the < operator, or provide stable_sort with a comparison function, eg.

stable_sort不知道如何对结构进行排序,因为没有为其定义的比较运算符。您要么需要创建一个类并覆盖 <操作符,或者提供一个具有比较函数的stable_sort(例如)。< p>

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

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

#2


1  

Well, how do you want to order your points? How do you know if a point should come before another point when sorted? Do you order by x coordinate, or what? You need to tell the compiler this!

那么,你想要点什么呢?你怎么知道一个点是否应该在另一个点之前排序?你点的是x坐标还是什么?你需要告诉编译器这个!

In other words, you need to provide an operator<.

换句话说,您需要提供一个操作符<。