#include<iostream>
#include<algorithm>
#include <vector>
#include <iterator>
using namespace std;
struct PAIR { int first; int second; };
std::ostream& operator<<(std::ostream& os, const PAIR& rec)
{
os << "(" << rec.first << ", " << rec.second << ")";
return os;
}
bool fun(const PAIR& x, const PAIR& y) { return x.first < y.first; }
int main()
{
vector<PAIR > v{ {1,3},{2,3}, {1,0}, {1,5} };
auto x1 = v;
cout << "v: ";
std::copy(v.begin(), v.end(), ostream_iterator< PAIR >(cout, ", "));
cout << "\n";
cout << "sorting now\n";
std::sort(v.begin(), v.end(), fun);
cout << "v: ";
std::copy(v.begin(), v.end(), ostream_iterator< PAIR >(cout, ", "));
cout << "\n";
}
output: v: (1, 3), (2, 3), (1, 0), (1, 5), sorting now v: (1, 3), (1, 0), (1, 5), (2, 3),
Changing bool fun(const PAIR& x, const PAIR& y) { return x.first < y.first; }
to bool fun(const PAIR& x, const PAIR& y) { return x.first <= y.first; }
is failing during runtime.
I have just changed < to <= in function fun(). Looks like there is some prerequisite for comparison function that we pass to STL sort algorithm.
Any help or pointers is greatly appreciated. Thanks in advance.