fork(1) download
  1. #include <algorithm>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. int n = 10;
  8. std::vector<std::vector<int>> A(2, std::vector<int>(n));
  9. int val;
  10. int i;
  11. while (std::cin >> val && i < 10)
  12. {
  13. A[0][i] = val;
  14. A[1][i] = i;
  15. ++i;
  16. }
  17.  
  18. // sort the index row, not the "data" row
  19. std::sort(A[1].begin(), A[1].end(),
  20. [&](int n1, int n2){ return A[0][n1] < A[0][n2]; });
  21. // output results
  22. for (int i = 0; i < 2; ++i)
  23. {
  24. for (int j = 0; j < n; ++j)
  25. {
  26. if (i == 0) // if this is a data row
  27. std::cout << A[i][A[1][j]] << " ";
  28. else // this is an index row
  29. std::cout << A[i][j] << " ";
  30. }
  31. std::cout << "\n";
  32. }
  33. }
  34.  
  35.  
Success #stdin #stdout 0s 3476KB
stdin
4 34 9 12 14 54 21 1 90 3
stdout
1 3 4 9 12 14 21 34 54 90 
7 9 0 2 3 4 6 1 5 8