Created
October 26, 2021 21:05
-
-
Save mclow/7c9c0eab6bd3bdc6d71bd956ecf4dd3c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <iostream> | |
#include <map> | |
#include <set> | |
#include <string> | |
#include <string> | |
#include <tuple> | |
using namespace std; | |
struct A { int i; }; bool operator<(const A&x, const A&y) { return x.i < y.i; } | |
struct B { float f; }; bool operator<(const B&x, const B&y) { return x.f < y.f; } | |
struct C { char c; }; bool operator<(const C&x, const C&y) { return x.c < y.c; } | |
typedef tuple<A, B, C> TABC; | |
typedef tuple<A, B> TAB; | |
struct TupCompare { | |
typedef void is_transparent; | |
bool operator() (const TABC &x, const TABC &y) const | |
{ return x < y; } | |
bool operator() (const TABC &x, const TAB &y) const { | |
if (get<0>(x) < get<0>(y)) return true; | |
if (get<0>(y) < get<0>(x)) return false; | |
if (get<1>(x) < get<1>(y)) return true; | |
if (get<1>(y) < get<1>(x)) return false; | |
return false; | |
} | |
bool operator() (const TAB &x, const TABC &y) const { | |
if (get<0>(x) < get<0>(y)) return true; | |
if (get<0>(y) < get<0>(x)) return false; | |
if (get<1>(x) < get<1>(y)) return true; | |
if (get<1>(y) < get<1>(x)) return false; | |
return false; | |
} | |
}; | |
int main () { | |
set<TABC, TupCompare> s2; | |
s2.emplace(A{1}, B{2.f}, C{'3'}); | |
s2.emplace(A{1}, B{2.f}, C{'4'}); | |
s2.emplace(A{1}, B{2.f}, C{'5'}); | |
s2.emplace(A{4}, B{5.f}, C{'6'}); | |
for (const auto el: s2) | |
cout << get<0>(el).i << ' ' << get<1>(el).f << ' ' << get<2>(el).c << endl; | |
auto eq = s2.equal_range(TAB(A{1}, B{2.f})); | |
cout << distance(eq.first, eq.second) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the heat of writing a comment, I did overstep in saying it doesn't work in practice. If it works for a scenario depends on the scenario.
The important point though is that there is a very critical change in behavior to operations like
find
and it's a type of error that is unexpected (usually it's not a concern if you passed in the right type of lookup key) and can never be flagged by a compiler. With no warning on the code sample or examples of issues, it feels like it's presented as a free lunch.