Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <functional>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <iomanip>
- struct House
- {
- std::string location;
- std::string type;
- double size;
- int value;
- };
- int main()
- {
- std::cout << "How do you wish to sort your houses?\n"
- "1: by location\n"
- "2: by size\n"
- "3: by value\n\n"
- "> ";
- int choice;
- std::cin >> choice;
- std::function<bool(const House&, const House&)> comparator;
- switch(choice) {
- case 1:
- comparator = [](const House& lhs, const House& rhs)
- { return lhs.location < rhs.location; };
- break;
- case 2:
- comparator = [](const House& lhs, const House& rhs)
- { return lhs.size < rhs.size; };
- break;
- case 3:
- comparator = [](const House& lhs, const House& rhs)
- { return lhs.value < rhs.value; };
- break;
- default:
- std::cout << "invalid selection";
- return -1;
- }
- std::vector<House> houses;
- std::ifstream in("sarasas.txt");
- House temp;
- std::string dump;
- while(in >> temp.location >> temp.type >> temp.size >> dump >> temp.value >> dump)
- houses.push_back(temp);
- std::sort(houses.begin(), houses.end(), comparator);
- for(const auto& f: houses)
- std::cout << std::setw(10) << f.location << std::setw(10) << f.type <<
- '\t' << f.size << '\t' << f.value << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement