qq_37712831 2023-04-21 16:05 采纳率: 66.7%
浏览 26
已结题

C++ List 这段编码为啥通不过


class Person
{
public:
    Person(string name, float score)
    {
        this->m_Name = name;
        this->m_Score = score;
    }

    bool operator==(const Person& p1) const

    {
        if (this->m_Name == p1.m_Name && this->m_Score==p1.m_Score)
            return true;
        return false;

    }

    string m_Name;
    float m_Score;
};


void setPlayer(vector<Person>& v)
{
    for (int i = 0; i < 5; i++)
    {
        string nameSeed = "ABCDE";
        string name = "选手";
        name += nameSeed[i];
        float score = 0;
        Person p(name, score);
        v.push_back(p);  
        
    }
    cout << endl;
}

void setPlayer(list<Person>& L)
{
        for (int i = 0; i < 5; i++)
        {
            string nameSeed = "FGHIJ";
            string name = "选手";
            name += nameSeed[i];
            float score = float(i*10+10);
            Person p(name, score); 
            L.push_back(p);  
        }
        for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
        {
            cout << (*it).m_Name << " " << "分数 " << setiosflags(ios::fixed) << setprecision(1)<< (*it).m_Score << endl;
         }
         cout << endl;
}


void setScore(vector<Person>& v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {

        deque<float> d1;
        cout << "给" << (*it).m_Name << "打分结果:    ";

        for (int i = 0; i < 10; i++)
        {

            float ret = rand() % 4 + 6 + rand()/(double)(RAND_MAX);
            d1.push_back(ret);
            cout << setiosflags(ios::fixed)<<setprecision(1)<< d1[i]<<" ";
        }
        cout << endl;
        sort(d1.begin(), d1.end());   
        d1.pop_front();
        d1.pop_back();
        float avg;
        float sum = 0;
        cout << "处理后的分数-" << (*it).m_Name << ": ";
        for (int i = 0; i < d1.size(); i++)
        {
            sum += d1[i];
            cout << setprecision(1)<<d1[i] << " ";
        }
        cout << endl;
        avg = float(sum / d1.size());
        (*it).m_Score = avg;
        cout << "平均分 " << setprecision(1)<<(*it).m_Score << endl;

    }
    cout << endl;
}
bool myCompare(Person& p1, Person& p2) 
{
    return p1.m_Score <  p2.m_Score;
}

int main()
{
    srand(time(NULL));
    vector<Person> v1;
    list<Person> L;
    setPlayer(v1);
    setPlayer(L);
    
    
    setScore(v1);
    sort(v1.begin(), v1.end(), myCompare); 
    cout << "按得分排序:" << endl;
    for (int i = 0; i < v1.size(); i++)
    {
        cout << v1[i].m_Name<<": "<< setprecision(1)<< v1[i].m_Score << endl;

    }
    cout << endl<<"冠军是:"<<endl;

    for (int i = 0; i < 5; i++)
    {   
        {
            if (v1[i].m_Score == v1[4].m_Score)

                cout << v1[i].m_Name << " " << setprecision(1) << v1[i].m_Score << " " << endl << endl; }

    }
    
    list<Person> L;
    Person p("选手H", 30);
    auto it1 = L.begin();
    //for (int i = 0, j = L.size(); i < j; i++)
    {
        Person person = *it1;
    if(person == p)
        {
             L.erase(it1); 
        }
        else it1++;
        
    }

    for (list<Person>::iterator it1 = L.begin(); it1 != L.end(); it1++)
    {
    
        cout << (*it1).m_Name << " " << (*it1).m_Score << endl;
    }

    
    system("pause");
    return 0;

}
  • 写回答

4条回答 默认 最新

  • 浪客 2023-04-21 16:12
    关注

    103行127行重定义L了。
    for那块修改下,

            // list<Person> L;
            Person p("选手H", 30);
            auto it1 = L.begin(); // auto it1 = L.begin();
            // for (int i = 0, j = L.size(); i < j; i++)
            for (auto it2 = L.begin(); it2 != L.end();)
            {
                    // Person person = *it1;
                    if (*it2 == p) // if (person == p)
                    {
                            L.erase(it2);
                            it2 = it1; // 删除后,从前一个位置重新遍历
                    }
                    else
                    {
                            it1 = it2;
                            it2++;
                    }
            }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月29日
  • 已采纳回答 4月21日
  • 创建了问题 4月21日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀