题解 | #牛牛的书#
牛牛的书
https://www.nowcoder.com/practice/30bb969e117b4f6d934d4b60a2af7489
#include <algorithm> #include <iostream> #include <vector> using namespace std; class Book { public: string name; double price; Book(string& name, double price): name(name), price(price) {} }; bool book_sort(Book& a, Book& b) { return a.price < b.price; } int main() { int n; cin >> n; vector<Book> v; while (n--) { string name; double price; cin >> name >> price; Book b(name, price); v.push_back(b); } sort(v.begin(), v.end(), book_sort); for (auto b : v) cout << b.name << endl; }