fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. #include <iomanip>
  5. #include <valarray>
  6.  
  7. int main()
  8. {
  9. {
  10. std::vector<int> seq { 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65 } ;
  11. for( int v : seq ) std::cout << v << ' ' ;
  12. std::cout << " (values)\n" ;
  13.  
  14. for( std::size_t pos = 0 ; pos < seq.size() ; ++pos )
  15. std::cout << std::setw(2) << pos << ' ' ;
  16. std::cout << " (original order)\n" ; ;
  17.  
  18. std::vector<std::size_t> order { 1, 9, 5, 7, 11, 2, 3, 8, 4, 6, 0, 10 } ;
  19. for( std::size_t pos : order ) std::cout << std::setw(2) << pos << ' ' ;
  20. std::cout << " (required order)\n" ; ;
  21.  
  22. std::vector< std::reference_wrapper<int> > indirect ;
  23. for(std::size_t pos : order ) indirect.emplace_back( seq[pos] ) ;
  24. for( int v : indirect ) std::cout << v << ' ' ;
  25. std::cout << " (indirect)\n" ; ;
  26. }
  27.  
  28. {
  29. std::valarray<int> seq { 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65 } ;
  30. std::valarray<std::size_t> order { 1, 9, 5, 7, 11, 2, 3, 8, 4, 6, 0, 10 } ;
  31. std::valarray<int> indirect { seq[order] } ;
  32. for( int v : indirect ) std::cout << v << ' ' ;
  33. std::cout << " (indirect)\n" ; ;
  34. }
  35. }
  36.  
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
10 15 20 25 30 35 40 45 50 55 60 65  (values)
 0  1  2  3  4  5  6  7  8  9 10 11  (original order)
 1  9  5  7 11  2  3  8  4  6  0 10  (required order)
15 55 35 45 65 20 25 50 30 40 10 60  (indirect)
15 55 35 45 65 20 25 50 30 40 10 60  (indirect)