Skip to content

Instantly share code, notes, and snippets.

@bobsomers
Created August 23, 2018 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobsomers/da17e7bd05836b9a15214c8294e51ac8 to your computer and use it in GitHub Desktop.
Save bobsomers/da17e7bd05836b9a15214c8294e51ac8 to your computer and use it in GitHub Desktop.
Example of using a custom sorting predicate function in C++
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
// Must return true if the first argument (a) should appear before the second argument (b).
bool interval_by_start(const Interval& a, const Interval& b) {
return a.start < b.start;
}
void merge(vector<Interval>& intervals) {
// Sort the intervals by their starting value, using a custom comparison function.
std::sort(intervals.begin(), intervals.end(), interval_by_start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment