samedi 16 juin 2018

I am trying to find the top 10 objects of a vector with pointers to the objects [on hold]

Following is the code I tried to do this:

void info(vector <Port *>& p)
{
    int pos=0;
    float max;
    vector <Port *> top;

    if (p.size()<=10)
        for (int i=0; i<p.size(); i++)
            p[i] -> print();
    else
    {
        for (int i=0; i<p.size(); i++)
            top.push_back(p[i]);
        for (int i=0; i<10; i++)
        {
            max=top[0]->calc();
            for (int j=1; j<top.size(); j++)
            {
                if ((top[j]->calc())>max)
                {
                    max=top[j]->calc();
                    pos=j;
                }
            }
            top[pos]->print();
            top.erase(top.begin()+pos);
        }
    }
}

The Port class contains some strings and a vector of pointers to some objects of other class which contains some floats. I need to find the 10 Port objects with the biggest value. The calc function finds the sum of the objects of the vector and returns it. I need to find the top 10 of the vector p depending on the calc function of Port. But I can't find why this doesn't work it properly.

Aucun commentaire:

Enregistrer un commentaire