Hello community, Happy New Year!↵
↵
I came up with interesting macro which allows sort objects by some field. The code is laconic but requires C++11. Macro itself:↵
↵
~~~~~↵
#define by(T, x) [](const T& a, const T& b) { return a.x < b.x; }↵
~~~~~↵
↵
Usage:↵
↵
↵
~~~~~↵
struct Item {↵
int a, b;↵
};↵
↵
sort(arr, arr + N, by(Item, a));↵
~~~~~↵
↵
Full example: http://pastebin.com/Cp5ZkwE4.↵
↵
Happy New Year!↵
↵
**UPD:** It was pointed out (in Russian discussion) that C++14 allows shorter version:↵
↵
~~~~~↵
#define by(x) [](auto a, auto b) { return a.x < b.x; }↵
sort(arr, arr + N, by(a));↵
~~~~~
↵
I came up with interesting macro which allows sort objects by some field. The code is laconic but requires C++11. Macro itself:↵
↵
~~~~~↵
#define by(T, x) [](const T& a, const T& b) { return a.x < b.x; }↵
~~~~~↵
↵
Usage:↵
↵
↵
~~~~~↵
struct Item {↵
int a, b;↵
};↵
↵
sort(arr, arr + N, by(Item, a));↵
~~~~~↵
↵
Full example: http://pastebin.com/Cp5ZkwE4.↵
↵
Happy New Year!↵
↵
**UPD:** It was pointed out (in Russian discussion) that C++14 allows shorter version:↵
↵
~~~~~↵
#define by(x) [](auto a, auto b) { return a.x < b.x; }↵
sort(arr, arr + N, by(a));↵
~~~~~