i have data structure consists of 3 int
values represent coordinate, , double
represents value @ coordinate. store them together, , sort them on value. values not unique. right now, have them in struct
, sort them using lambda, shown in code below. piece of performance-critical code, looking implementation gives fastest sorting. list contain 10^6 10^7 elements.
what elegant way solve this? not trying use std::sort
, asking whether store data in struct
best solution, or there better alternatives?
#include <vector> #include <algorithm> #include <iostream> struct data { int i; int j; int k; double d; }; int main() { std::vector<data> v; v.push_back({1,2,3,0.6}); v.push_back({1,2,3,0.2}); v.push_back({1,2,3,0.5}); v.push_back({1,2,3,0.1}); v.push_back({1,2,3,0.4}); std::sort(v.begin(), v.end(), [](const data& a, const data& b) { return a.d < b.d; }); (auto d : v) std::cout << d.i << ", " << d.j << ", " << d.k << ", " << d.d << std::endl; return 0; }
the fastest way sort them not have sort them.
at expense of slower insertion, store entire container sorted, , insert in correct place. std::set here, or roll own.
edit: std::multiset provide same advantages if need allow values compare equal.