我将很感谢帮助我解决这个问题。 我需要在我的类中使用vector来保存数据并存储它们。 假设我没有使用矢量。 请指导我怎么做?
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;
}
};
答案 0 :(得分:0)
如果我找对你,这是一个存储你的班级数据的向量。
#include <iostream>
#include <vector>
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 main() {
vector<House> vec;
for (int i = 0; i < 5; ++i){
House temp;
temp.Getdata();
vec.push_back(temp);
}
for (int i = 0; i < vec.size(); ++i)
vec[i].Putdata();
return 0;
}