fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <unordered_map>
  5. #include <vector>
  6.  
  7. int main()
  8. {
  9. using vi = std::vector<int>;
  10. using vf = std::vector<float>;
  11.  
  12. vi indices = {2, 4, 8, 1};
  13. vf values = {1.5, 0.5, 2.5, 4.5};
  14.  
  15. std::unordered_map<int, float> correspondences;
  16. std::transform(begin(indices), end(indices), begin(values),
  17. std::inserter(correspondences, end(correspondences)),
  18. [](int i, float v) { return std::make_pair(i, v); });
  19. std::sort(begin(indices), end(indices),
  20. [&correspondences](int l, int r) {
  21. return correspondences.at(l) < correspondences.at(r);
  22. });
  23. std::copy(begin(indices), end(indices), std::ostream_iterator<int>(std::cout, ", "));
  24. }
Success #stdin #stdout 0s 4336KB
stdin
Standard input is empty
stdout
4, 2, 8, 1,