[英]Reading from a file to a vector
我正在从文件中读取以下数据。 我需要实现的是最后在向量m_hoteli中填充具有CTurist对象的CHotel对象。 第二到第五排是带有变量的酒店,后面是大酒店(500,400,300,600),即每家酒店的游客人数。
当我运行它时,矢量将填充文件中的信息,但会将游客的详细信息填充为酒店数据,因此无法在输入和CTurist之间建立正确的连接。
码头5500 Joe 21 1 Tisho 20 6 Victoria 31 20
郁金香4400莎拉41 17罗西塔49 14瓦莱里亚24 2
黑海3300约翰35 12乔恩35 11詹尼28 6
SwissBell 5600奥林26 1玛格丽塔27 2朱丽叶31
class CComplex:CHotel
{
protected:
string m_complex;
vector<CHotel> m_hoteli;
public:
CComplex(){};
CComplex(string filename, string nComplex)
{
string str;
m_complex = nComplex;
fstream file(filename, ios::in);
if (file.is_open())
{
CHotel temp(" ", 0, 0);
while (file >> temp)
{
m_hoteli.push_back(temp);
}
file.close();
}
else
throw "ERROR! ";
}
CHotel(String stringname)构造函数:
class CHotel : public CTurist //втори клас, наследник на CTurist
{
protected:
string hName; //име хотел
int stars; //звезди на хотела
int beds; //брой легла
public:
map<CTurist, unsigned> Turisti;
unsigned Sum = 0;
int br = 0;
CHotel(){};
CHotel(string s)
{
map<CTurist, unsigned> TR;
bool first = true;
istringstream TList(s);
int i = 0;
while (getline(TList, s, ' '))
{
switch (i)
{
case 0: this->setName(s); break;
case 1: this->setAge(stoi(s)); break;
}
if (i ==2 )
{
if (!first){ setName(getName()); setAge(getAge()); first = true; }
else{ TR[CTurist("", 0)]; }
i = -1;
}
i++;
}
Turisti = TR;
}
我认为这是我在构造函数上取得的一些进步。 从断点处看,酒店会收到它的信息,也就是名字,星星和床,但游客却没有。 有小费吗?
CHotel(string s){
CTurist b("",0);
bool first = true;
istringstream TList(s);
string Days;
int i = 0;
string ime;
string godini;
if (first == true)
{
while (getline(TList, s, ' '))
{
switch (i)
{
case 0: this->hName = s; break;
case 1:this->stars = stoi(s); break;
case 2:this->beds = stoi(s); break;
}
i++;
if (i == 3)break;
}
first = false;
i = 0;
}
while (getline(TList, s, '|'))
{
switch (i)
{
case 0: ime = s; break;
case 1: godini = s; break;
case 2: Days = s; break;
}
i++;
if (i == 3)
{
CTurist T1("ime", stoi(Days));
//Turisti.insert(pair<CTurist,unsigned>("ime", stoi(Days)));
i = 0;
}
}
}
如何将T1插入mapTuristi ???
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.