Copying from map to a list of pointers

半世苍凉 提交于 2019-12-10 12:07:11

问题


I have this interesting assignment where I have a std::map of CTurist (previous class) and unsigned variable. Here's the code:

class CTurist
{
protected:
    string tName;
    int age;

public:
    CTurist() {};

    CTurist(string name, int age2)
    {
        tName = name;
        age = age2;
    }


    bool operator<(const CTurist& e) const
    {
        return age < e.age;
    }

    friend ostream& operator<<(ostream& os, const CTurist&& e);
    friend ifstream& operator>>(ifstream& is, CTurist&& e);
};

class CHotel:public CTurist
{
protected:

    string hName;
    int stars;
    int beds;
    map<CTurist, unsigned> Turisti;

public:
    unsigned Sum = 0;

    CHotel(){};

    CHotel(string hName2, int zvezdi, int legla)
    {
        hName = hName;
        stars = zvezdi;
        beds = legla;
    }



    int Compare()
    {
        list<CTurist*> list;

        int br=0;
        CTurist vyzrast;
        map<CTurist, unsigned>::iterator it = Turisti.begin();
        while (it != Turisti.end())
        {
            if (it->first < vyzrast)
            {
                br++;
            }
            else
            {
                list.push_back(std::move(&it->first));
            }
        }
    }
};

I know it's quite a long one, but I thought it's best to give you all the information.

Now, the int Compare() function AT THE BOTTOM is the one causing me problems.

I have to check if the age of the tourists is above or below a parameter which I've called vyzrast here. We're comparing the age. If it's below, it's quite straight forward.

If it's above though, I have to add those Tourists to a list<CTurist*>, aka. to a list of pointers. If I make the list from objects, not pointers. No luck with this, hence why I'm looking for suggestions how to fix it here.


回答1:


Why are you using rvalue references for your operator overloads? You should be using const CTurist & for operator<< and CTurist& for operator>>. And you should be using std::istream for operator>>:

friend ostream& operator<<(ostream& os, const CTurist &e);
friend istream& operator>>(istream& is, CTurist &e);

Aside from that, there is no reason for Compare() to use std::move() at all when populating the std::list, as it is adding pointers, not moving actual objects.

Compare() doesn't won't correctly for several reasons:

  1. it->first is an actual const CTurist object, but your std::list is expecting CTurist* pointers. std::map keys are const, so &it->first is a CTurist const * pointer (non-const pointer to const object), not a CTurist* pointer (non-const pointer to non-const object) like you are expecting.

  2. Your vyzrast object is uninitialized. You are not assigning any value to its age (and tName) member at all, so the result of your comparisons is indeterminate.

  3. You are not incrementing your it iterator on each loop iteration, so you are stuck in an infinite loop if the std::map is not empty.

Try something more like this instead:

int Compare()
{
    std::list<const CTurist *> clist;

    int br = 0;
    CTurist vyzrast("", SomeAgeValueHere);

    std::map<CTurist, unsigned>::iterator it = Turisti.begin();
    while (it != Turisti.end())
    {
        if (it->first < vyzrast)
        {
            br++;
        }
        else
        {
            clist.push_back(&it->first);
        }
        ++it;
    }

    // use clist as needed...

    return SomeValueHere;
}

Live Demo



来源:https://stackoverflow.com/questions/50109501/copying-from-map-to-a-list-of-pointers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!