fork(10) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. template <typename T>
  6. std::vector<std::size_t> compute_order(const std::vector<T>& v)
  7. {
  8. std::vector<std::size_t> indices(v.size());
  9. std::iota(indices.begin(), indices.end(), 0u);
  10. std::sort(indices.begin(), indices.end(), [&](int lhs, int rhs) {
  11. return v[lhs] < v[rhs];
  12. });
  13. std::vector<std::size_t> res(v.size());
  14. for (std::size_t i = 0; i != indices.size(); ++i) {
  15. res[indices[i]] = i;
  16. }
  17. return res;
  18. }
  19.  
  20. int main() {
  21. const auto order = compute_order<int>({40, 20, 10, 30});
  22. for (const auto& e : order) {
  23. std::cout << " " << e;
  24. }
  25. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
 3 1 0 2