从文件读取到向量

时间:2018-05-01 19:33:54

标签: c++ file class vector stdmap

我正从文件中读取以下数据。我需要实现的是在最后用带有CTurist对象的CHotel对象填充向量m_hoteli。第2至第5行是带有变量的酒店,在大数字(500,400,300,600)之后,是每个酒店的游客。

当我运行它时,我的向量充满了来自文件的信息,但是将游客的详细信息填充为酒店数据,因此我无法在输入和CTurist之间建立正确的连接。

  

Marina 5 500 Joe 21 1 Tisho 20 6 Victoria 31 20

     

Tulip 4 400 Sarah 41 17 Rositsa 49 14 Valeria 24 2

     

BlackSea 3 300 John 35 12 Jon 35 11 Janni 28 6

     

SwissBell 5 600 Orlin 26 1 Margarita 27 2 Juliette 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;
        }

1 个答案:

答案 0 :(得分:0)

我认为我在构造函数上做了一些进步。通过断点,酒店收到它的信息,即名字,星星和床,但游客不会。有什么提示吗?   CHotel(字符串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 ???

相关问题