如何保持类的条目以排序和显示它们?c ++

时间:2014-03-14 21:01:47

标签: c++ class sorting

我为房屋经理写了一些代码。 我希望保持用户的条目按一个对象排序并显示它们。 我知道如何对数据进行排序,但首先我需要在类中保留数据以在排序之前和之后显示。 保存数据的最简单方法是什么?

#include "stdafx.h"
#include <iostream>
using namespace std;

class House
{
      private :
             int NumRooms;
             int Area;
             int FloorNumber;
      public :

             void Getdata()
             {
                   cout << "Enter number of rooms in house: ";
                   cin >> NumRooms;
                   cout << "Enter area of house: ";
                   cin >> Area;
                   cout << "Enter floor number of house: ";
                   cin >> FloorNumber;
             }
             void Putdata()
             {
                   cout  << "Number of rooms: " << NumRooms << endl;
                   cout << "Area of house: " << Area << endl;
                   cout << "Floor number: " << FloorNumber << endl;
            }
};

int menu ();

int _tmain(int argc, _TCHAR* argv[])
{
    int n;
    House s;
    switch(menu())
    {
    case 1 :
        cout << "How many houses:";
        cin >> n;
        if(n > 20)
            cout << "Number of houses is more than max.20!!!" << endl;
        else
        {
            for(int i=1; i<=n; i++)
            {
                cout << "Details of " << i << " house" << endl;
                s.Getdata();
            }
        }
        menu();

    case 2 :
        menu();

    case 3 :
            s.Putdata();
            menu();

    default :
    cout << "End of program!!!" << endl;
    }

    system("pause");
    return 0;
}

int menu()
{
    int c;
    cout << "Wlcome to house manager! Choose one of actions" << endl;
    cout << "1. Enter data (max.20 houses)" << endl;
    cout << "2. Sort houses" << endl;
    cout << "3. Display list" << endl;
    cout << "4. Exit" << endl;
    cout << "Choice: ";
    cin >> c;
    return c;
}

2 个答案:

答案 0 :(得分:0)

最简单的方法就是将它们放入std::vector<House>

答案 1 :(得分:0)

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class House
{
      private :
             int NumRooms;
             int Area;
             int FloorNumber;
      public :
             bool operator<(const House& other) 
             {
                 return (this->Area < other.Area);
             }

             void Getdata()
             {
                   cout << "Enter number of rooms in house: ";
                   //cin >> NumRooms;
                   cout << "Enter area of house: ";
                   //cin >> Area;
                   cout << "Enter floor number of house: ";
                   //cin >> FloorNumber;
                   NumRooms= rand() * 10;
                   Area = NumRooms * rand();
                   FloorNumber=rand();
             }
             void Putdata()
             {
                   cout  << "Number of rooms: " << NumRooms << endl;
                   cout << "Area of house: " << Area << endl;
                   cout << "Floor number: " << FloorNumber << endl;
            }
};

int menu ();
typedef std::vector<House> houseCollection;

int _tmain(int argc, _TCHAR* argv[])
{
    int n;
    House s;
    houseCollection houses;
    switch(menu())
    {
    case 1 :
        cout << "How many houses:";
        //cin >> n;
        n=5;
        if(n > 20)
            cout << "Number of houses is more than max.20!!!" << endl;
        else
        {
            for(int i=1; i<=n; i++)
            {
                cout << "Details of " << i << " house" << endl;
                s.Getdata();
                houses.push_back(s);
            }
        }
        menu();

    case 2 :
        std::sort(houses.begin(), houses.end());
        menu();

    case 3 :

        for( houseCollection::iterator it = houses.begin(); it != houses.end(); it++)
        {
            s = (*it);
            s.Putdata();
        }
        menu();

    default :
    cout << "End of program!!!" << endl;
    }

    system("pause");
    return 0;
}

int menu()
{
    int c;
    cout << "Wlcome to house manager! Choose one of actions" << endl;
    cout << "1. Enter data (max.20 houses)" << endl;
    cout << "2. Sort houses" << endl;
    cout << "3. Display list" << endl;
    cout << "4. Exit" << endl;
    cout << "Choice: ";
    cin >> c;
    return c;
}