You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

01-comparison.cc 621 B

123456789101112131415161718192021222324252627282930
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. struct CppRecord {
  5. size_t id;
  6. double value;
  7. };
  8. int main() {
  9. std::vector<CppRecord> records = {
  10. {1, 3.5},
  11. {2, 2.0},
  12. {3, 3.5},
  13. {4, 1.0},
  14. {5, 2.0}};
  15. std::sort(records.begin(), records.end(), [](const CppRecord& a, const CppRecord& b) {
  16. if (a.value != b.value) {
  17. return a.value < b.value;
  18. } else {
  19. return a.id < b.id;
  20. }
  21. });
  22. for (const auto& record : records) {
  23. std::cout << record.id << " " << record.value << std::endl;
  24. }
  25. return 0;
  26. }