我写了一个函数,它将采用点矢量,点是一个结构,并将使用稳定排序对其进行排序,我得到以下错误:
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;
}
任何帮助请
答案 0 :(得分:3)
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);
答案 1 :(得分:1)
那么, 你想要点什么?你怎么知道一个点在排序之前是否应该出现在另一个点之前?你是按x坐标订购的,还是什么?你需要告诉编译器这个!
换句话说,您需要提供运算符&lt; 。