关于 sort函数问题
#include<iostream>#include<algorithm>
bool bigThan(const long & num1,const long & num2)
{
if(num1 >= num2)
{
return true;
}
else
{
return false;
}
}
int reduce(long ar[], int n)
{
long *tail;
std::sort(ar, ar+n,bigThan);/*单步到这一句崩溃了*/
tail = std::unique(ar,ar+n);
return (tail - ar);
}
int main()
{
long num = {1,2,3,4,5,5,4,3,2,1};
std::cout << reduce(num, 10) << '\n';
return 0;
}
排序准则返回true的时候要严格弱序(简单说就是不能含等号)
#include<iostream>
#include<algorithm>
bool bigThan(const long & num1,const long & num2)
{
//if(num1 >= num2)
if(num1 > num2)
{
return true;
}
else
{
return false;
}
}
int reduce(long ar[], int n)
{
long *tail;
std::sort(ar, ar+n,bigThan);/*单步到这一句崩溃了*/
tail = std::unique(ar,ar+n);
return (tail - ar);
}
int main()
{
long num = {1,2,3,4,5,5,4,3,2,1};
std::cout << reduce(num, 10) << '\n';
return 0;
}
页:
[1]