Last active
June 2, 2016 17:41
-
-
Save max-dark/43a28e42da1d433b2a484df02511c57a to your computer and use it in GitHub Desktop.
Создание массива функторов для сравнения по полям структуры
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 <functional> | |
#include <algorithm> | |
#include <iostream> | |
#include <utility> | |
#include <string> | |
#include <vector> | |
#include <assert.h> | |
template <class type> | |
using compare = std::function<bool(const type&, const type&)>; | |
template< | |
class classT, | |
class memberT, | |
class member_ptrT = memberT classT::*, | |
class cmpT = compare<memberT> | |
> | |
compare<classT> make_cmp( | |
memberT classT::*member, | |
cmpT cmp = std::less<memberT>() | |
) { | |
return [cmp, member](const classT& a, const classT& b) -> bool { | |
return cmp(a.*member, b.*member); | |
}; | |
} | |
template< class classT > | |
struct cmp { | |
using cmpT = compare < classT > ; | |
template< class memberT, class cmp_funcT = compare<memberT>> | |
static cmpT make(memberT classT::*member, cmp_funcT cmp_func = std::less<memberT>()) { | |
return [cmp_func, member](const classT& a, const classT& b) -> bool { | |
return cmp_func(a.*member, b.*member); | |
}; | |
} | |
}; | |
struct test_idx { | |
char one; | |
int two; | |
std::string three; | |
test_idx* four; | |
}; | |
struct test_idx2 { | |
char one; | |
int two; | |
std::string three; | |
test_idx* four; | |
}; | |
namespace type_checks { | |
static_assert( | |
std::is_same < | |
decltype(make_cmp(&test_idx::one)), | |
decltype(cmp<test_idx>().make(&test_idx::one)) > ::value, | |
"Разные типы"); | |
} | |
int main() { | |
using namespace std; | |
vector<test_idx> a{ | |
{'a', 100500, "hello", nullptr}, | |
{'z', 42, "a hell", (test_idx*)(0xdead)} | |
}; | |
vector<test_idx2> b{ | |
{ 'a', 100500, "hello", nullptr }, | |
{ 'z', 42, "a hell", (test_idx*)(0xdead) } | |
}; | |
vector<compare<test_idx>> | |
idx_a{ | |
make_cmp(&test_idx::one), | |
make_cmp(&test_idx::two), | |
make_cmp(&test_idx::three), | |
make_cmp(&test_idx::four) | |
}; | |
vector<compare<test_idx2>> | |
idx_b{ | |
make_cmp(&test_idx2::one), | |
make_cmp(&test_idx2::two), | |
make_cmp(&test_idx2::three), | |
make_cmp(&test_idx2::four) | |
}; | |
for (const auto& cmp : idx_a) { | |
std::sort(a.begin(), a.end(), cmp); | |
for (const auto& i : a) | |
cout << '{' | |
<< i.one << ", " | |
<< i.two << ", " | |
<< i.three << ", " | |
<< i.four << "} "; | |
cout << endl; | |
} | |
cout << endl; | |
for (const auto& cmp : idx_b) { | |
std::sort(b.begin(), b.end(), cmp); | |
for (const auto& i : b) | |
cout << '{' | |
<< i.one << ", " | |
<< i.two << ", " | |
<< i.three << ", " | |
<< i.four << "} "; | |
cout << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment