Как стереть одни и те же элементы в структуре с помощью вектора - PullRequest
0 голосов
/ 07 мая 2020
#include <iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

struct Student {
    string name;
    string number;
    double score;
};

bool compare(const Student& a, const Student& b) {
    return a.score > b.score;
}

int main()
{
    vector<Student> v;
    Student student;

    int x = 500;
    Student stu[x];
    int i = 0;


    do {
        cout<<"Please enter the name of the student.: ";
        getline(cin, stu[i].name,'\n');
        // is the name properly typed in?"
        while(stu[i].name == "") {
            cout<<"Please enter a proper name.: ";
            getline(cin, stu[i].name,'\n');
        }
        if(stu[i].name == "QUIT") {
            break;
        }

        cout<<"Please enter the student number.: ";
        cin>>stu[i].number;

        cout<<"Please enter the score.: ";
        cin>>stu[i].score;
        // is the score in the range?
        while(stu[i].score <= 0 || stu[i].score >=100) {
            cout<<"The score must be in the range of 0~100. \n"
                <<"Please enter a proper score.: ";
            cin>>stu[i].score;
        }
        i++;
        cin.ignore();
        v.push_back(student);


    } while (stu[i-1].name != "0" && stu[i-1].number != "0" && stu[i-1].score != 0);

    sort(stu, stu+i, compare);
    v.erase(unique(v.begin(), v.end()), v.end());
}

Я хочу сначала отсортировать элементы, и, если они введены, стереть их. Я пытался использовать v.erase (unique (v.begin (), v.end ()), v.end ()), но это не сработало. Не могу понять, в чем проблема. Я что-то упустил?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...