#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::sort;
struct node
{
int x, y;
};
bool cmp(int a, int b)//double also works
{
return a > b;
}
bool cmp(node a, node b)
{
if (a.x != b.x)
{
return a.x > b.x;
}
else
{
return a.y > b.y;
}
}
int main()
{
int array[5] = {1, 2, 3, 4, 5};
sort(array, array+5);
for (int i = 0; i < 5; i++)
{
cout << array[i];
}
sort(array, array+5, cmp);
for (int i = 0; i < 5; i++)
{
cout << array[i];
}
vector <node> stu;
stu[0].x = 2;
stu[0].y = 1;
stu[1].x = 2;
stu[1].y = 3;
stu[2].x = 4;
stu[2].y = 0;
sort(stu.begin(), stu.end(), cmp);
for (int i = 0; i < 3; i++)
{
cout << stu[i].x << " " << stu[i].y << endl;
}
}
error: no matching function for call to 'sort'
sort(array, array+5, cmp);
error: no matching function for call to 'sort'
sort(stu.begin(), stu.end(), cmp);
这段代码报错
为什么会错,sort也可以用于int类型数组呀