Advertisement
Naohiro19

優先順位を付けて並べ替える

Jan 25th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <tuple>
  6.  
  7. struct Book {
  8. std::string name;
  9. std::string publisher;
  10. std::int32_t price;
  11.  
  12. Book(const std::string& name, const std::string& publisher, std::int32_t price)
  13. : name(name), publisher(publisher), price(price) {}
  14. };
  15.  
  16. bool operator<(const Book&a, const Book& b) {
  17. return std::tie(a.name, a.publisher, a.price) < std::tie(b.name, b.publisher, b.price);
  18. }
  19.  
  20.  
  21. int main()
  22. {
  23. std::vector<Book> books = {
  24. {"ポケットプログレッシブ英和辞典〔第3版〕","小学館", 1750},
  25. {"スクリプト言語による効率的ゲーム開発", "ソフトバンククリエイティブ",3800}
  26. };
  27.  
  28. std::sort(books.begin(), books.end());
  29.  
  30. for(const Book& book: books) {
  31. std::cout << book.name << "[" << book.publisher << "]" << book.price << "円" << std::endl;
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement