fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <algorithm>
  5.  
  6. int main()
  7. {
  8. int intArray[] = {5, 3, 32, -1, 1, 104, 53};
  9.  
  10. std::vector<std::pair<int, size_t>> arrayValueIndexPairs;
  11.  
  12. for( size_t i = 0; i < sizeof(intArray) / sizeof(intArray[0]); ++i )
  13. {
  14. arrayValueIndexPairs.push_back(std::pair<int, size_t>(intArray[i], i));
  15. }
  16.  
  17. std::sort( arrayValueIndexPairs.begin(), arrayValueIndexPairs.end() );
  18.  
  19. std::cout << "Sorted Array looks like this." << std::endl;
  20.  
  21. for( const auto& it : arrayValueIndexPairs )
  22. {
  23. std::cout << "First Value \'" << it.first << "\' and Second Value \'" << it.second << "\'" << std::endl;
  24. }
  25.  
  26. return EXIT_SUCCESS;
  27. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Sorted Array looks like this.
First Value '-1' and Second Value '3'
First Value '1' and Second Value '4'
First Value '3' and Second Value '1'
First Value '5' and Second Value '0'
First Value '32' and Second Value '2'
First Value '53' and Second Value '6'
First Value '104' and Second Value '5'