fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. // Function used to compare two ints
  6. // Note that the comparison will just compare a and b, nothing to do with time
  7. bool compareInt(int a, int b)
  8. {
  9. return a > b;
  10. }
  11.  
  12. int main(){
  13.  
  14. std::vector<int> time={5, 16, 4, 7};
  15. std::vector<int> amplitude={10,17,8,16};
  16. std::vector<int> index(time.size(), 0);
  17.  
  18. for (int i = 0 ; i != index.size() ; i++) {
  19. index[i] = i;
  20. }
  21.  
  22. // This sort method uses a function (compareInt) to compare the elements
  23. // Not that the result of the program will be wrong since it will just
  24. // compare the elements from index and not from the vector time
  25. sort(index.begin(), index.end(), compareInt);
  26.  
  27. std::cout << "Time \t Ampl \t idx" << std::endl;
  28. for (int ii = 0 ; ii != index.size() ; ++ii) {
  29. std::cout << time[index[ii]] << " \t " << amplitude[index[ii]] << " \t " << index[ii] << std::endl;
  30. }
  31. }
Success #stdin #stdout 0s 4576KB
stdin
Standard input is empty
stdout
Time 	 Ampl 	 idx
7 	 16 	 3
4 	 8 	 2
16 	 17 	 1
5 	 10 	 0