TimothyY
TimothyY

Reputation: 31

Print out values of a map by an iterator

Some pretext:

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())
        {
            string str;
            bool dfFlag = false;
            //CHotel temp(" ", 0, 0);
            //while (file >> temp)
            while (getline(file,str))
            {
                CHotel temp(str);
                m_hoteli.push_back(str);
            }
            file.close();
        }
        else
            throw "ERROR! ";
    }

File I'm reading from:

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|3

That's the way I fill m_hoteli vector with CHotel objects, which include CTurist object.

By using breakpoints and testing, I'm 100% certain that I receive the hotel values. It received the student values as well, but that I fail to test that. That's the code for that:

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;    //сменя на false, за да се изпълни долния while
            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));
                CTurist T1(ime, stoi(godini));
                Turisti[T1] = stoi(Days);
                //Turisti.insert(T1("ime", stoi(Days))=Days);
                i = 0;
            }
        }
    }
    ...
}

My problems is I can't list out the tourists which are part of each hotel. That's the function I have:

void outputInfo()
{
   for (int i = 0; i < m_hoteli.size(); i++) {
      cout << "Tourists for hotel " << m_hoteli[i].getName2() << endl;
      std::map<CTurist, unsigned>::iterator it = Turisti.begin();
      for (auto it = Turisti.begin(); it != Turisti.end(); ++it) {
         cout << " Tourist: " << m_hoteli[i].Turisti[it].getName()
              << " Vuzrast: " << m_hoteli[i].Turisti[it].getAge()<< endl;
      }
   }
}

In this line:

cout << " Tourist: " << m_hoteli[i].Turisti[it].getName();

I get the following error :

No operator[] matches these operands.

Any ideas what is the way to go here?

Upvotes: 0

Views: 118

Answers (1)

acraig5075
acraig5075

Reputation: 10756

You would fix the error by using the iterator correctly like this:

for (int i = 0; i < m_hoteli.size(); i++)
{
    std::map<CTurist, unsigned>::iterator it = m_hoteli[i].Turisti.begin();

    for (; it != m_hoteli[i].Turisti.end(); ++it)
    {
        cout << " Tourist: " << it->first.getName(); // ...
    }
}

But it'd be better like this

for (auto & hotel : m_hoteli)
{
    cout << "Tourists for hotel " << hotel.getName2() << endl;

    for (auto & mapElement : hotel.Turisti)
    {
        cout << " Tourist: " mapElement.first.getName(); // ...
    }
}

To get the days for the tourist you would use mapElement.second.

Upvotes: 1

Related Questions