Advertisement
Guest User

Untitled

a guest
Jun 5th, 2014
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <functional>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <fstream>
  7. #include <iomanip>
  8.  
  9. struct House
  10. {
  11.     std::string location;
  12.     std::string type;
  13.     double size;
  14.     int value;
  15. };
  16.  
  17. int main()
  18. {
  19.     std::cout << "How do you wish to sort your houses?\n"
  20.                  "1: by location\n"
  21.                  "2: by size\n"
  22.                  "3: by value\n\n"
  23.                  "> ";
  24.     int choice;
  25.     std::cin >> choice;
  26.     std::function<bool(const House&, const House&)> comparator;
  27.     switch(choice) {
  28.       case 1:
  29.         comparator = [](const House& lhs, const House& rhs)
  30.                             { return lhs.location < rhs.location; };
  31.         break;
  32.       case 2:
  33.         comparator = [](const House& lhs, const House& rhs)
  34.                             { return lhs.size < rhs.size; };
  35.         break;
  36.       case 3:
  37.         comparator = [](const House& lhs, const House& rhs)
  38.                             { return lhs.value < rhs.value; };
  39.         break;
  40.       default:
  41.         std::cout << "invalid selection";
  42.         return -1;
  43.     }
  44.     std::vector<House> houses;
  45.     std::ifstream in("sarasas.txt");
  46.     House temp;
  47.     std::string dump;
  48.     while(in >> temp.location >> temp.type >> temp.size >> dump >> temp.value >> dump)
  49.         houses.push_back(temp);
  50.     std::sort(houses.begin(), houses.end(), comparator);
  51.     for(const auto& f: houses)
  52.         std::cout << std::setw(10) << f.location << std::setw(10) << f.type <<
  53.                               '\t' << f.size << '\t' << f.value << '\n';
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement