C++ inbuilt sort function does not work when we use equals sign in its comparator function with large and repeating input. I have pasted the code here and in validate section if I use equals sign in (val1<=val2) so this does not work for large and repeating inputsbut if i use it without equals sign everything works fine. Can anyone explain why?
I have already tried the code in different compilers as well as different devices.The same problem persists there as well.
#include<bits/stdc++.h>
using namespace std;
bool validate(int val1,int val2)
{
if(val1<=val2)
return true;
else
return false;
}
int main()
{
int total_elements;
cin>>total_elements;
vector<int> nums(total_elements);
for(int i=0;i<total_elements;i++)
cin>>nums[i];
sort(nums.begin(),nums.end(),validate);
for(int i=0;i<total_elements;i++)
cout<<nums[i]<<" ";
}
Runtime error occurs and the processing stops in the middle but if i use it without equals sign everything works fine.