fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. struct PancakeStats
  6. {
  7. PancakeStats(int personId, int numberOfPancakes)
  8. : personId(personId)
  9. , numberOfPancakes(numberOfPancakes)
  10. {
  11.  
  12. }
  13. int personId;
  14. int numberOfPancakes;
  15.  
  16. bool operator < (const PancakeStats& other)
  17. {
  18. return numberOfPancakes < other.numberOfPancakes;
  19. }
  20. };
  21.  
  22. int main()
  23. {
  24. const auto numberOfInputs = 3;
  25. int numberOfPancakes = 0;
  26.  
  27. using IntPair_t = std::pair<int, int>;
  28. std::vector<PancakeStats> structVector;
  29. std::vector<IntPair_t> pairVector;
  30.  
  31. for (auto i = 0; i < numberOfInputs; ++i)
  32. {
  33. std::cin >> numberOfPancakes;
  34. structVector.push_back(PancakeStats(i + 1, numberOfPancakes));
  35. pairVector.push_back(std::make_pair(i + 1, numberOfPancakes));
  36. }
  37.  
  38. std::sort(structVector.begin(), structVector.end());
  39.  
  40. for (const auto& entry : structVector)
  41. {
  42. std::cout << entry.personId << " | " << entry.numberOfPancakes << std::endl;
  43. }
  44.  
  45. std::sort(pairVector.begin(), pairVector.end(), [](const IntPair_t &lhs,
  46. const IntPair_t &rhs)
  47. {
  48. return lhs.second < rhs.second;
  49. });
  50.  
  51. for (const auto& entry : pairVector)
  52. {
  53. std::cout << entry.first << " | " << entry.second << std::endl;
  54. }
  55.  
  56. return 0;
  57. }
Success #stdin #stdout 0s 3464KB
stdin
300
50000
10
stdout
3 | 10
1 | 300
2 | 50000
3 | 10
1 | 300
2 | 50000