Created
August 23, 2018 17:43
-
-
Save bobsomers/da17e7bd05836b9a15214c8294e51ac8 to your computer and use it in GitHub Desktop.
Example of using a custom sorting predicate function in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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