Last active
August 29, 2015 14:03
-
-
Save dmonopoly/30a3f4ea1a89a4dc9b68 to your computer and use it in GitHub Desktop.
Overriding operator< in C++ (http://coliru.stacked-crooked.com/a/dcbd4cf20810c3a4 & http://coliru.stacked-crooked.com/a/75fddbccc876ab94)
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
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
struct One { | |
string name; | |
vector<string> values; | |
}; | |
struct Two { | |
string name; | |
vector<One> one_values; | |
bool operator<(const Two& other) const | |
{ | |
return name < other.name; | |
} | |
}; | |
int main() { | |
vector<Two> twos; | |
std::sort(twos.begin(), twos.end()); | |
} |
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
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
struct One { | |
string name; | |
vector<string> values; | |
}; | |
struct Two { | |
string name; | |
vector<One> one_values; | |
}; | |
bool operator<(const Two& lhs, const Two& rhs) | |
{ | |
return lhs.name < rhs.name; | |
} | |
int main() { | |
vector<Two> twos; | |
std::sort(twos.begin(), twos.end()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment